使列表视图可滚动以仅适合屏幕的一部分
Making listview scrollable to fit only a portion of the screen
我想让我的列表视图只滚动到屏幕的一部分,我几乎没有其他设计要显示在屏幕底部,下面是我的代码,其中我添加了 layout-weight= 1 人 posts.I 已经引用了其他帖子。
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:text=" Computer Languages..." />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</ListView>
</LinearLayout>
最好的方法是使用 RelativeLayout
<RelativeLayout
...
>
<TextView
android:id="@+id/firstview"
...
/>
<ListView
android:layout_below="@+id/firstview"
android:layout_above="@+id/lastview"
...
/>
<TextView
android:id="@+id/lastView"
/>
</RelativeLayout>
只需将您的视图添加到同一 LinearLayout 中同一级别的 ListView 下方即可。也就是说,您可能希望在 ListView 中设置 layout_height="0dp"
(这样它的高度就不需要额外测量一次),从而提高性能。这对于 ListView 项特别有用。只要您保持其他视图的 layout_weights 未设置(或将它们设置为 0),您的 ListView 就会占用所有剩余的 space.
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
编辑: 下面是对 ListView 进行了更改的布局文件,作为测试,我添加了一个 TextView,它应该强制 ListView 给它 space 就是了。如果您在 phone 上的模拟器中测试此布局,或在 Eclipse / AS 中预览它,它会完全符合预期。
如果此布局不适合您,那么这里还有其他原因,也许您正在以编程方式修改布局,但没有额外的 info/code 可以查看,我不能说。
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:text=" Computer Languages..." />
<ListView android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:text="test test text" />
</LinearLayout>
我想让我的列表视图只滚动到屏幕的一部分,我几乎没有其他设计要显示在屏幕底部,下面是我的代码,其中我添加了 layout-weight= 1 人 posts.I 已经引用了其他帖子。
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:text=" Computer Languages..." />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_weight="1" >
</ListView>
</LinearLayout>
最好的方法是使用 RelativeLayout
<RelativeLayout
...
>
<TextView
android:id="@+id/firstview"
...
/>
<ListView
android:layout_below="@+id/firstview"
android:layout_above="@+id/lastview"
...
/>
<TextView
android:id="@+id/lastView"
/>
</RelativeLayout>
只需将您的视图添加到同一 LinearLayout 中同一级别的 ListView 下方即可。也就是说,您可能希望在 ListView 中设置 layout_height="0dp"
(这样它的高度就不需要额外测量一次),从而提高性能。这对于 ListView 项特别有用。只要您保持其他视图的 layout_weights 未设置(或将它们设置为 0),您的 ListView 就会占用所有剩余的 space.
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
编辑: 下面是对 ListView 进行了更改的布局文件,作为测试,我添加了一个 TextView,它应该强制 ListView 给它 space 就是了。如果您在 phone 上的模拟器中测试此布局,或在 Eclipse / AS 中预览它,它会完全符合预期。
如果此布局不适合您,那么这里还有其他原因,也许您正在以编程方式修改布局,但没有额外的 info/code 可以查看,我不能说。
<LinearLayout 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"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:text=" Computer Languages..." />
<ListView android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textStyle="bold"
android:text="test test text" />
</LinearLayout>