【问题标题】:Android custom dialog layout doesn't workAndroid自定义对话框布局不起作用
【发布时间】:2012-02-20 06:32:53
【问题描述】:

我不明白为什么我的对话框布局表现得很奇怪。如果我在最后一个小部件中使用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();
}

【问题讨论】:

  • 在此链接中查看我的答案stackoverflow.com/questions/8894300/…如果您喜欢,请点赞
  • 有同样的问题,虽然我不得不使用 fill_parent 因为它是 1.6。确实很奇怪,但可能与窗口上的窗口布局计算有关......

标签: android layout dialog


【解决方案1】:

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

【讨论】:

  • 我已经在两个设备上测试了代码:Samsung Galaxy SL I9003 (Android 2.3.7) 和GT-N8000 (3G & Wifi) (Android 4.1.2)。除非我将android:layout_width="wrap_content" 用于Samsung Galaxy SL I9003 上的按钮,否则对话框是可以的; android:layout_width="wrap_content"android:layout_width="match_parent" 在平板电脑上运行良好
【解决方案2】:

我遇到了同样的问题。无论我将布局及其子控件设置为什么,我的对话框都非常狭窄。不过,我可以通过在 Eclipse 中进行干净的构建来解决这个问题。

【讨论】:

    猜你喜欢
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多