【问题标题】:Setting width and height to AlertDialog with custom layout doesn't apply correctly使用自定义布局将宽度和高度设置为 AlertDialog 无法正确应用
【发布时间】:2021-06-13 20:19:52
【问题描述】:

我的对话框中的文本太长,并且与按钮混合在一起。所以我想我应该改变对话框的大小。

upd.: 为了清楚起见,问题是是否可以以这种方式更改警报对话框的大小,以便将这些更改应用于布局,以及如何?如果真的不可能,为什么以及有什么替代方案。

根据答案in this topic,这是我尝试在代码中执行此操作的方法:

    int matchParent = WindowManager.LayoutParams.MATCH_PARENT;
    rulesDialog.show();
    rulesDialog.getWindow().setLayout(matchParent, matchParent);

我得到了这个,它只是增加了空白:

也试过this。那根本行不通。 我也尝试用同样的方法添加这一行,什么也没做:

<item name="android:layout_height">match_parent</item>

这是传递给AlertDialog.Builder的布局的xml代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/beige">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:padding="4dp"
        android:layout_margin="4dp"
        android:textColor="@color/brown"
        android:textSize="14sp"
        android:text="@string/info_text"
        android:id="@+id/info_text"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/info_text"
        android:layout_centerHorizontal="true"
        android:text="@string/ok"
        android:padding="4dp"
        android:textSize="14sp"
        android:textColor="@color/orange"
        android:background="@color/blank"
        android:minHeight="0dp"
        android:minWidth="0dp"
        android:layout_margin="4dp"
        android:id="@+id/ok"/>


    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/info_text"
        android:layout_toEndOf="@id/ok"
        android:text="@string/renew_record"
        android:padding="4dp"
        android:textSize="14sp"
        android:textColor="@color/orange"
        android:background="@color/blank"
        android:minWidth="0dp"
        android:minHeight="0dp"
        android:layout_margin="4dp"
        android:id="@+id/renew_record"/>

</RelativeLayout>

upd.:我发现这段代码有效:

rulesDialog.show();
int matchParent = WindowManager.LayoutParams.MATCH_PARENT;
rulesDialog.getWindow().setLayout(matchParent, matchParent);
rulesDialog.setContentView(R.layout.info);

彻底解决白缝问题。 在某些情况下,重叠的问题仍然存在。据我了解,这是RelativeLayout的常见问题,我正在调查。

更新: 是的,我应该从TextView 中删除android:layout_centerVertical="true" 行。现在一切正常,后两次修复,问题解决了。

【问题讨论】:

    标签: android android-layout android-widget


    【解决方案1】:

    您可以在 .xml 中使用滚动视图,也可以在 textview 本身中启用滚动。我认为后者更简单,如下所示。

    <TextView
            android:id="@+id/info_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ....  
            android:maxLines="7"   <-- play with this number to best suit your expectations-->
            android:scrollbars="vertical"
            />
    

    最后在您的 .java/.kt 文件中。

    textView.setMovementMethod(new ScrollingMovementMethod());
    

    【讨论】:

    • 这可能是一个解决方案。但是,我想知道是否可以正确更改 AlertDialog 的大小以供将来使用。
    • 嗯,奇怪,我试过这个,按钮仍然与文本重叠,即使对话框的大小变小了。
    • 您是否厌倦了为根元素(相对布局)设置 height = wrap_content 以根据您的文本大小调整对话框的大小。
    • 相对于约束布局/线性布局改变,它可能有助于解决重叠问题。
    • 设置 height = wrap_content 不起作用,因为 AlertDialog 会转储布局根的布局参数,这就是为什么我们通常在扩展布局时将 null 作为父级传递。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 2021-10-16
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    相关资源
    最近更新 更多