【问题标题】:Android: What should the width of a dialog be?Android:对话框的宽度应该是多少?
【发布时间】:2011-06-23 18:21:53
【问题描述】:

我正在使用 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> 

我发现了很多关于让对话框在宽度上填满屏幕的问题,但没有关于让它在宽度上换行的问题。我怎样才能得到这个?

【问题讨论】:

    标签: android dialog


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

      对话框的一般样式是与包含子对象所需的大小一样大。如果孩子没有占据整个宽度,对话框也不会。

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

      或者,如果您的对话框真的那么简单,只需使用AlertDialog

      【讨论】:

        猜你喜欢
        • 2011-02-07
        • 1970-01-01
        • 2012-07-19
        • 1970-01-01
        • 2014-07-13
        • 1970-01-01
        • 1970-01-01
        • 2018-02-25
        • 2012-09-25
        相关资源
        最近更新 更多