【发布时间】:2014-08-14 04:12:20
【问题描述】:
我要创建一个底部有两个按钮的列表视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="@android:id/list"></ListView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_weight="1"
android:text="Add Task"
android:id="@+id/addTaskButton"></Button>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="1"
android:text="Add Task2"
android:id="@+id/addTaskButton2"></Button>
</LinearLayout>
</LinearLayout>
如何使列表视图与剩余父级的高度匹配?
如果我为列表视图设置高度
android:layout_height="400dp"
这并不适合所有设备
如果我设置 match_parent
android:layout_height="match_parent"
列表视图将覆盖底部的按钮
【问题讨论】:
-
利用
android:layout_weight参数,不管屏幕大小,都可以做相应的调整。 -
然后使用RelativeLayout。在列表视图下方对齐包含按钮的线性布局,并将其对齐在父视图的底部。你有相对布局的参数来实现这一点。
-
上面的代码没有错..尝试使用 hieght as wrap 运行它
-
使用RealativeLayout作为父级并赋予按钮布局属性“android:layout_alignParentBottom=true”和listview布局属性“android:layout_above="id of button layout"
标签: android android-layout android-listview android-linearlayout