Android 自定义对话框布局不起作用

标签 android layout dialog

我不明白为什么我的对话框布局表现得很奇怪。如果我对最后一个小部件使用 android:layout="wrap_content",它会使对话框变得非常狭窄。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="20dp">

    <TextView 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam bibendum leo eget eros mattis nec eleifend nulla elementum. Suspendisse suscipit suscipit enim in mollis. "    
    />

    <CheckBox 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Don't remind me later"    
    />

    <Button 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button"    
    />

</LinearLayout>

Java 代码。

private void showDialog() {
    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.dial);
    dialog.setTitle("Dialog title");
    dialog.show();
}

最佳答案

Match_parent 表示此小部件的大小将是父级的大小...因此,如果父级有 200 像素,那么小部件也将有。相反,如果您使用 wrap_content...意味着小部件的大小将与其内容相等。因此,如果 TextView 包含大约 50 像素长的文本,那么这将是小部件的大小。 基于此定义,您现在可以构建更好的布局。

关于Android 自定义对话框布局不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9357078/

相关文章:

android - 在 Android RelativeLayout maxWidth 中不起作用

Android:自动为xml中的所有ID创建变量

c++ - 在对话框窗口上使用图标 C++ Win32 API

android - AlertDialog 和警告消息 "Window Already Focused"

java - 更新到Android 3.6.1后出现错误

Android限制微调器下拉列表中显示的项目数

android - 获取 Android 中以编程方式生成的按钮网格的 ID

java - Android Studio MainActivity 标记为红色错误。小白

android - 线性布局切割 Material 卡阴影

jQuery UI 对话框 - 如何使其不可关闭?