我正在使用我自己的 Dialog
子类,并尝试以这样一种方式构建它:如果没有足够的内容来填充屏幕,它将缩小以适合内容(即 wrap_content
).我可以使用以下布局来做到这一点:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialog_bg">
<RelativeLayout
android:id="@+id/title_container"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:background="@drawable/dialog_title_bg">
. . .
</RelativeLayout>
<LinearLayout
android:id="@+id/button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/content"
android:orientation="horizontal">
. . .
</LinearLayout>
<FrameLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title_container">
</FrameLayout>
</RelativeLayout>
(我删除了我认为不重要的部分)。当内容没有填满屏幕时,这很有效:
但是当内容很大时,它会将按钮推离底部:
稍作调整,我更改了 button_container
,使其具有 layout_alignParentBottom="true"
并且 content
在上方 它,像这样:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/dialog_bg">
<RelativeLayout
android:id="@+id/title_container"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:background="@drawable/dialog_title_bg">
. . .
</RelativeLayout>
<LinearLayout
android:id="@+id/button_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
. . .
</LinearLayout>
<FrameLayout
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/title_container"
android:layout_above="@+id/button_container">
</FrameLayout>
</RelativeLayout>
现在它适用于内容对于屏幕来说太大的情况:
但是当内容没有填满屏幕时,对话框仍然填满:
我怎样才能两全其美?我可能可以通过将“maxHeight”设置为屏幕高度减去对话框边框以及标题和按钮的高度来在代码中完成此操作,但这对我来说似乎有点老套。我试着查看 AlertDialog 的实现(它似乎可以正确处理这种情况),但对我来说它看起来像是黑魔法...
编辑:根据要求,这是我添加到表示对话框“内容”的FrameLayout
的布局文件的内容:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:id="@+id/report_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
</RelativeLayout>
最佳答案
而不是框架布局,更好地使用线性。它会工作..如果你发布 xml 代码然后我可以检查..
关于android - 自定义对话框中的 ListView 将按钮推离屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13966949/