Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android对话框高度_Android_Android Layout_Android Dialog - Fatal编程技术网

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,该对话框保持在那里,而布局的其余部分可以滚动)