如何使 DialogFragment 包装其内容并同时将视图对齐到底部?
How to make a DialogFragment wrap its content and align a view to the bottom at the same time?
我想构建一个 DialogFragment
,其中包含一个 ListView
和一个底部 View
(包含一些按钮)。
ListView
可以显示可变数量的项目,因此具有 可变高度。
- 底部
View
有固定高度。
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<!-- Any kind of view. e.g. Button... -->
<View
android:id="@+id/myBottomView"
android:layout_width="match_parent"
android:layout_height="50dp" >
</View>
</LinearLayout>
只要 ListView
仅包含几个项目并且对话框的总高度不大于屏幕尺寸,使用 LinearLayout
就可以。在这种情况下,对话框会自动包装其内容,并且只达到它需要的高度。
但是,当 ListView
包含大量项目并且尺寸超过屏幕尺寸时,对话框会自动调整到其最大高度并且底部视图不再可见。
这可以通过使用 RelativeLayout
作为 root 来解决:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/myBottomView" >
</ListView>
<!-- Any kind of view. e.g. Button... -->
<View
android:id="@+id/myBottomView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true" >
</View>
</RelativeLayout>
这解决了之前描述的问题:底部视图始终在底部可见,无论 ListView
有多高。但是,现在对话框总是占用它的最大值。高度并没有调整到 ListView
?
的实际高度
所以每个布局都解决了问题的一部分,但不能同时解决两个问题。如何在底部同时获得自动调整大小和底部布局?
编辑:
ListView 只需要 android:layout_weight="1"
。
所以你的布局应该是这样的。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:layout_weight="1" <!-- this will force listview to expand according to it's height.. -->
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<View
android:id="@+id/myBottomView"
android:layout_width="match_parent"
android:layout_height="50dp" >
</View>
</LinearLayout>
我想构建一个 DialogFragment
,其中包含一个 ListView
和一个底部 View
(包含一些按钮)。
ListView
可以显示可变数量的项目,因此具有 可变高度。- 底部
View
有固定高度。
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<!-- Any kind of view. e.g. Button... -->
<View
android:id="@+id/myBottomView"
android:layout_width="match_parent"
android:layout_height="50dp" >
</View>
</LinearLayout>
只要 ListView
仅包含几个项目并且对话框的总高度不大于屏幕尺寸,使用 LinearLayout
就可以。在这种情况下,对话框会自动包装其内容,并且只达到它需要的高度。
但是,当 ListView
包含大量项目并且尺寸超过屏幕尺寸时,对话框会自动调整到其最大高度并且底部视图不再可见。
这可以通过使用 RelativeLayout
作为 root 来解决:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/myBottomView" >
</ListView>
<!-- Any kind of view. e.g. Button... -->
<View
android:id="@+id/myBottomView"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true" >
</View>
</RelativeLayout>
这解决了之前描述的问题:底部视图始终在底部可见,无论 ListView
有多高。但是,现在对话框总是占用它的最大值。高度并没有调整到 ListView
?
所以每个布局都解决了问题的一部分,但不能同时解决两个问题。如何在底部同时获得自动调整大小和底部布局?
编辑:
ListView 只需要 android:layout_weight="1"
。
所以你的布局应该是这样的。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:layout_weight="1" <!-- this will force listview to expand according to it's height.. -->
android:id="@+id/myListView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<View
android:id="@+id/myBottomView"
android:layout_width="match_parent"
android:layout_height="50dp" >
</View>
</LinearLayout>