【问题标题】:TableLayout within DialogFragment does not respect "wrap_content" attributeDialogFragment 中的 TableLayout 不尊重“wrap_content”属性
【发布时间】:2014-04-22 15:20:46
【问题描述】:

我正在尝试用 3 个 TableRows 绘制一个 TableLayout。该表在扩展 DialogFragment 的 xml 文件(见下文)中定义。表格的所有 3 行都有

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>

【问题讨论】:

    标签: android xml android-tablelayout


    【解决方案1】:

    当我将最外层的 LinearLayout 替换为 RelativeLayout 时,它起作用了。 所以,我知道如何解决它,但我仍然不明白为什么会这样。欢迎添加其他 cmets。

    【讨论】:

    • 我也有同样的问题。所以我用相对替换了根布局。现在它正在工作。感谢您发布解决方案老兄。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-26
    • 2011-01-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多