2015-10-02 57 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