2015-10-02 55 views
0

我試圖用自定義XML佈局,看起來像這樣來顯示AlertDialog:的Android,AlertDialog調整換行自定義佈局內容

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

<TextView 
    android:id="@+id/sort_dialog_title" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:background="@color/cyan" 
    android:gravity="center_horizontal" 
    android:padding="16dp" 
    android:text="Sort by" 
    android:textColor="@color/white" 
    android:textSize="34sp" 
    android:textStyle="bold" /> 

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:padding="16dp"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="left" 
     android:paddingBottom="8dp" 
     android:paddingTop="8dp" 
     android:text="Upcoming" 
     android:textSize="24sp" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="left" 
     android:paddingBottom="8dp" 
     android:paddingTop="8dp" 
     android:text="Oldest" 
     android:textSize="24sp" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="left" 
     android:paddingBottom="8dp" 
     android:paddingTop="8dp" 
     android:text="Newest" 
     android:textSize="24sp" /> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="left" 
     android:paddingBottom="8dp" 
     android:paddingTop="8dp" 
     android:text="Category" 
     android:textSize="24sp" /> 

</LinearLayout> 

</LinearLayout> 

我想警告對話框,以不佔用的80%屏幕默認爲,而只應佔用LinearLayout所需的空間,以便它可以很好地縮放。在我的活動片段我有這樣的:

LayoutInflater inflater = getLayoutInflater(savedInstanceState); 
      View dialoglayout = inflater.inflate(R.layout.sort_dialog_view, null); 
      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
      builder.setView(dialoglayout); 
      AlertDialog window = builder.show(); 
      window.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 

然而,最後有沒有效果的時刻,對話框顯示與我左邊的LinearLayout,佔40%的對話框,再大的白色空間在右邊。我不知道如何解決這個問題。

回答

0

每個TextView使用match_parent,這意味着它的默認值爲其父寬度是wrap_context哪個輪流依賴於它的孩子。

你不能兩者都做,所以你需要決定是否要他們來匹配他們的父母,在這種情況下,家長需要一個指定的寬度(例如200dp),或者如果TextViews換行。聽起來就像你想要每個TextView的寬度爲wrap_content

+0

這真是愚蠢的我,但仍然沒有適當調整窗口大小。即使將所有設置設置爲wrap_content,LinearLayout仍然位於右側的大白塊旁邊。 –

+0

默認情況下可能會有minWidth,請嘗試'android:minWidth = 0dp'? – RobVoisey