2012-02-20 66 views
1

我不明白为什么我的对话框布局行为很奇怪。如果我使用android:layout="wrap_content"作为最后一个小部件,它会使对话非常窄。Android自定义对话框布局不起作用

<?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(); 
} 
+0

看到我的答案在这个链接http://stackoverflow.com/questions/8894300/fully-custom-dialog-in-android-with-the-same-look-regardless-device/8894444#8894444.and upvote if你喜欢 – Sameer 2012-02-20 07:02:03

+0

有同样的问题,但我不得不使用fill_parent,因为它是1.6。奇怪的确如此,但可能与窗口的窗口布局计算有关... – codeScriber 2012-02-20 07:04:12

回答

1

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

+0

我已经测试了两个设备上的代码:[Samsung Galaxy SL I9003](http://en.wikipedia.org/wiki/ Samsung_Galaxy_SL_I9003)(Android 2.3.7)和[GT-N8000(3G&Wifi)](http://en.wikipedia.org/wiki/Samsung_Galaxy_Note_10.1)(Android 4.1.2)。除非我在_Samsung Galaxy SL I9003_上使用'android:layout_width =“wrap_content”'作为对话框, 'android:layout_width =“wrap_content”'和'android:layout_width =“match_parent”'在平板电脑上运行良好 – 2013-11-02 10:27:52

0

我有同样的问题。无论我如何设置布局和其子控件,我的对话都很窄。尽管我在Eclipse中做了一个干净的构建,但我能够解决这个问题。