列表视图直到最后一项才滚动



您好,我在滚动时遇到问题,直到最后一个项目滚动在中间停止,这是我的布局代码。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="fragment.Equivalent">
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:focusable="false"/>
<TextView
android:id="@+id/txt404"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Message that will be shown if the listview is empty"
android:textSize="21dp"
android:textStyle="bold"
android:visibility="invisible" />
</RelativeLayout>

尝试使用滚动视图:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="fragment.Equivalent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:scrollbarStyle="insideOverlay"
style="@android:style/Widget.Holo.ScrollView">
<TableLayout
android:id="@+id/table_layout_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
</TableLayout>
</ScrollView>
</RelativeLayout>

在 Java 文件中,执行以下操作:

TableLayout ll = (TableLayout) findViewById(R.id.table_layout_id);
TableRow row = new TableRow(this);
    
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
    
row.setLayoutParams(lp);

    
    butt = new Button(this);
    //You can add button too, FYI
butt.setText("TEST");
    
    
    
TextView tv = (TextView) findViewById(R.id.txt404);

tv.setText("Text view");
    
tv2 = new TextView(this);
    //your job: add list view here
tv2.setText("Text View 2");
    
    
    
row.addView(butt);

    row.addView(tv1);
    
row.addView(tv2);
    
    
ll.addView(row, i); // where i is the row number, increment on addition

解决方案是将布局更改为约束布局并更改列表视图的大小

<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="fragment">
<ListView
android:id="@+id/listView"
android:layout_width="399dp"
android:layout_height="485dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="0dp"
android:focusable="false"
android:visibility="visible"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintTop_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="@+id/txt404"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="21dp"
android:textStyle="bold"
android:visibility="invisible"
tools:layout_constraintTop_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintBottom_creator="1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:layout_constraintLeft_creator="1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintVertical_bias="0.391" />
</android.support.constraint.ConstraintLayout>

最新更新