Android对话框高度
我试图设置一个简单的对话框,但我似乎无法控制高度。它总是最大屏幕高度。有什么方法可以让它正确地包装到内容中吗Android对话框高度,android,android-layout,android-dialog,Android,Android Layout,Android Dialog,我试图设置一个简单的对话框,但我似乎无法控制高度。它总是最大屏幕高度。有什么方法可以让它正确地包装到内容中吗 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save"
android:id="@+id/buttonSave"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:textAlignment="center"
style="?attr/borderlessButtonStyle"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@android:string/cancel"
android:id="@+id/buttonCancel"
android:layout_alignParentBottom="true"
android:layout_toStartOf="@+id/buttonSave"
style="?attr/borderlessButtonStyle"/>
</RelativeLayout>
您的XML中有
android:layout\u alignParentBottom=“true”
。这将强制您的按钮
位于其可用空间的最底部,从而有效地使RelativeLayout
具有一个匹配父项
布局高度
行为,而不是包装内容
行为
另一种方法是使用线性布局
或网格布局
。
类似于这一点的东西会起作用:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--
The rest of your layout can go here
with a layout_height of 0dp and a layout_weight of 1
if you want your buttons to dock themselves at the bottom of the dialog
-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@android:string/cancel"
android:id="@+id/buttonCancel"
style="?attr/borderlessButtonStyle"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save"
android:id="@+id/buttonSave"
android:textAlignment="center"
style="?attr/borderlessButtonStyle"/>
</LinearLayout>
</LinearLayout>
您的XML中有
android:layout\u alignParentBottom=“true”
。这将强制您的按钮
位于其可用空间的最底部,从而有效地使RelativeLayout
具有一个匹配父项
布局高度
行为,而不是包装内容
行为
另一种方法是使用线性布局
或网格布局
。
类似于这一点的东西会起作用:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<!--
The rest of your layout can go here
with a layout_height of 0dp and a layout_weight of 1
if you want your buttons to dock themselves at the bottom of the dialog
-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@android:string/cancel"
android:id="@+id/buttonCancel"
style="?attr/borderlessButtonStyle"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/save"
android:id="@+id/buttonSave"
android:textAlignment="center"
style="?attr/borderlessButtonStyle"/>
</LinearLayout>
</LinearLayout>
尝试添加dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_父项、ViewGroup.LayoutParams.WRAP_内容)显示对话框之前,请执行代码>。适用于DialogFragment
。请注意:您的XML中有android:layout\u alignParentBottom=“true”
。这将强制将按钮放置在其可用空间的最底部,从而有效地使RelativeLayout
具有匹配的父布局高度行为,而不是环绕内容。为什么不在android:orientation=“horizontal”
上使用LinearLayout
?这是为数不多的几件我没有调整的事情之一,所以这可能有点道理,但如果是这样的话:多么糟糕的设计!我之所以使用RelativeLayout
是因为对话框中还有十几个东西,我很少使用LinearLayout
处理复杂布局(嵌套布局的惩罚等)。以上是我在最终使用“WTF!?!?!?”之前将其精简到的内容,哈哈。我将在大约两小时后进行测试并跟进。@Patrick您对alignParentBottom
的看法确实正确。我将把它作为bug提交,因为这基本上是未定义的行为。一个孩子让家长忽略了它的布局设置。是的,确实有点奇怪。我想我会为其他有同样问题的人写一份正式答复。您能够发布完整的XML文件吗?根据你想要达到的目标,有不同的方法来解决这个问题。(例如,当我第一次遇到这个问题时,我想在对话框的底部放置一个AppBarLayout,而其余的布局可以滚动)尝试添加dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_父项,ViewGroup.LayoutParams.WRAP_内容)显示对话框之前,请执行代码>。适用于DialogFragment
。请注意:您的XML中有android:layout\u alignParentBottom=“true”
。这将强制将按钮放置在其可用空间的最底部,从而有效地使RelativeLayout
具有匹配的父布局高度行为,而不是环绕内容。为什么不在android:orientation=“horizontal”
上使用LinearLayout
?这是为数不多的几件我没有调整的事情之一,所以这可能有点道理,但如果是这样的话:多么糟糕的设计!我之所以使用RelativeLayout
是因为对话框中还有十几个东西,我很少使用LinearLayout
处理复杂布局(嵌套布局的惩罚等)。以上是我在最终使用“WTF!?!?!?”之前将其精简到的内容,哈哈。我将在大约两小时后进行测试并跟进。@Patrick您对alignParentBottom
的看法确实正确。我将把它作为bug提交,因为这基本上是未定义的行为。一个孩子让家长忽略了它的布局设置。是的,确实有点奇怪。我想我会为其他有同样问题的人写一份正式答复。您能够发布完整的XML文件吗?根据你想要达到的目标,有不同的方法来解决这个问题。(例如,当我第一次遇到这个问题时,我想在对话框底部放置一个AppBarLayout,该对话框保持在那里,而布局的其余部分可以滚动)