【发布时间】:2012-12-20 07:08:42
【问题描述】:
我正在使用我自己的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>
【问题讨论】:
-
您找到解决方案了吗?我正在经历同样的事情。
-
@StuartRobertson 不幸的是没有,我最终使用我在问题中提到的
maxHeight东西在代码中做到了。
标签: android android-layout dialog android-dialog customdialog