Android:对话框的宽度应该是多少?

时间:2011-02-08 22:01:11

标签: android dialog

我正在使用android:theme =“@ android:style / Theme.Dialog”的活动来使其充当对话框。什么最接近对话框的原生行为:对话框是否应填充屏幕(宽度)?例如,如果通过键入填充文本框,它是否会动态增长?

我的预感是对话框不应该填满屏幕。我试图得到这种行为,但无论我做什么,在Android 2.2上,对话框宽度似乎填充了父

我的布局xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView android:id="@+id/text" android:layout_width="fill_parent" 
        android:layout_height="wrap_content" android:autoText="true" 
        android:text="@string/item_delete_dialog_label" 
        android:padding="5dip" />

    <LinearLayout android:layout_height="wrap_content" 
        android:orientation="horizontal"
        style="@android:style/ButtonBar" android:layout_width="fill_parent">

        <Button android:id="@+id/yes" android:layout_height="wrap_content" 
            android:text="@string/button_yes"
            android:layout_width="wrap_content"
            android:layout_weight="1"/>

        <Button android:id="@+id/no" android:layout_height="wrap_content" 
            android:text="@string/button_no"
            android:layout_width="wrap_content"
            android:layout_weight="1"/>

    </LinearLayout>
</LinearLayout> 

我发现很多关于使对话框在宽度上填充屏幕的问题,但没有关于使其以宽度包裹的问题。我怎么能得到这个?

2 个答案:

答案 0 :(得分:0)

对话框的一般风格应该与他们的孩子一样大。如果孩子不占用整个宽度,则对话框也不会。

您的问题可能是您的LinearLayout的宽度为fill_parent。这意味着它将占用尽可能多的空间。更改wrap_content的宽度,因此它不会占用超出需要的空间。

或者,如果您的对话框非常简单,只需使用AlertDialog

答案 1 :(得分:0)

ButtonBar样式使对话框比使用标准按钮更宽。将LinearLayout宽度更改为wrap_content不会影响对话框的宽度,因为它只是填充设置为wrap_content的主LinearLayout容器 - 更改按钮样式肯定会产生影响。

相关问题