2012-02-20 67 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中做了一個乾淨的構建,但我能夠解決這個問題。