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

时间:2012-02-20 06:32:53

标签: 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();
}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

我有同样的问题。我的对话框非常狭窄,无论我将布局及其子控件设置为什么。我能够通过在Eclipse中进行干净的构建来解决问题。