1
我试图用3个TableRows绘制一个TableLayout。该表是在一个xml文件中定义的(见下文),它扩大了DialogFragment。表的所有3行都有DialogFragment中的TableLayout不尊重“wrap_content”属性
layout_height = "wrap_content"
并且每行的高度计算正确。但是,封闭的TableLayout包含了一些大的余量,并没有像应该那样“包装”它的内容。我在这里做错了什么?
<?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">
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/borderless_blue_5dp" >
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:padding="5dp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="@string/CHANGE_ROOM"
android:textSize="20sp" />
</TableRow>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/web_safe_silver" />
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:padding="5dp" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="@string/CLIENT"
android:textSize="@dimen/text_size_regular" />
<TextView
android:id="@+id/tv_client_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:background="@drawable/dark_rect_smooth"
android:textColor="@color/discoveroom_gray"
android:textSize="@dimen/text_size_regular" />
</TableRow>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/web_safe_silver" />
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/border"
android:weightSum="10"
android:padding="5dp" >
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="@string/ROOM"
android:textSize="16sp" />
<TextView
android:id="@+id/room_number"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:background="@drawable/dark_rect_smooth"
android:textColor="@color/discoveroom_gray"
android:textSize="@dimen/text_size_regular" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="@string/CHARGE"
android:textSize="@dimen/text_size_regular" />
<TextView
android:id="@+id/charge"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:background="@drawable/dark_rect_smooth"
android:textColor="@color/discoveroom_gray"
android:textSize="@dimen/text_size_regular"/>
</TableRow>
</TableLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/web_safe_silver" />
<ListView
android:id="@+id/alternative_rooms_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:choiceMode="singleChoice"
android:visibility="invisible" >
</ListView>
<LinearLayout
android:id="@+id/edit_buttons"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="invisible" >
<Button
android:id="@+id/btn_close"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@string/CANCEL"
android:textColor="#894556"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="@+id/btn_ok"
style="?android:attr/buttonBarButtonStyle"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@string/OK"
android:textColor="#489456"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
我也有同样的问题。所以我用Relative替换了根布局。现在它正在工作。感谢您发布解决方案。 –