Content is not wrapping when layout

I have created a simple table with varies length of text. I'm using layout_height=wrap_content in my table row. If you see the picture, it has a wide spaces between the next element and the previous one. If I set the height, then the spaces will remained when viewed in landscape. I don't know where to fixed it. I mostly used styles... so it's weird that some are working and some not.

示例应用程序

sampleLayout.xml

    <TableLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="90dp"
        android:background="@color/form_background"
        android:shrinkColumns="*"
        android:stretchColumns="*">

        <!--Table header-->
        <TableRow
            style="@style/table_row"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/th_component"
                style="@style/table_header"
                android:layout_height="match_parent"
                android:layout_weight="2.8"
                android:text="@string/tableH_komponen"/>

            <TextView
                android:id="@+id/th_marks"
                style="@style/table_header"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="@string/tableH_markah"/>

            <TextView
                android:id="@+id/th_demerits"
                style="@style/table_header"
                android:layout_height="match_parent"
                android:layout_weight="0.7"
                android:text="@string/tableH_demerit"/>

            <TextView
                android:id="@+id/th_notes"
                style="@style/table_header"
                android:layout_height="match_parent"
                android:layout_weight="0.5"
                android:text="@string/tableH_catatan"/>
        </TableRow>

        <!--Component C1-->

        <TableRow
            style="@style/table_row"
            android:layout_height="wrap_content">

            <TextView
                style="@style/table_attr"
                android:text="@string/c1"/>

            <TextView
                android:id="@+id/tv_cC1"
                style="@style/table_component"
                android:text="@string/cC1"
                />

            <TextView
                android:id="@+id/tv_mc1"
                style="@style/table_marks"
                android:layout_height="wrap_content"
                android:text="@string/_6"/>

            <CheckBox
                android:id="@+id/checkBox_c1"
                style="@style/table_demerit"
                android:layout_height="wrap_content"/>

            <ImageButton
                android:id="@+id/btn_c1"
                style="@style/table_notes"
                android:layout_height="wrap_content"
                android:text="@string/catatan"/>
        </TableRow>

        <!--Component C2-->

        <TableRow
            style="@style/table_row"
            android:layout_height="wrap_content">

            <TextView
                style="@style/table_attr"
                android:text="@string/c2"/>

            <TextView
                android:id="@+id/tv_cC2"
                style="@style/table_headComponent"
                android:text="@string/cC2"/>

            <ImageButton
                android:id="@+id/btn_c2"
                style="@style/table_notes"
                android:layout_height="wrap_content"
                android:text="@string/catatan"/>
        </TableRow>

        <TableRow
            style="@style/table_row"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/tv_cC2_1"
                style="@style/table_subComponent"
                android:text="@string/cC2_1"/>

            <TextView
                android:id="@+id/tv_mC2_1"
                style="@style/table_marks"
                android:layout_height="match_parent"
                android:text="@string/_1"/>

            <CheckBox
                android:id="@+id/checkBox_c2_1"
                style="@style/table_subDemerit"
                android:layout_height="wrap_content"/>
        </TableRow>

        <TableRow
            style="@style/table_row"
            android:layout_height="wrap_content">

            <TextView
                android:id="@+id/tv_cC2_2"
                style="@style/table_subComponent"
                android:layout_height="wrap_content"
                android:text="@string/cC2_2"/>

            <TextView
                android:id="@+id/tv_mC2_2"
                style="@style/table_marks"
                android:layout_height="wrap_content"
                android:text="@string/_1"/>

            <CheckBox
                android:id="@+id/checkBox_c2_2"
                style="@style/table_subDemerit"
                android:layout_height="wrap_content"/>
        </TableRow>
    </TableLayout>

</FrameLayout>

strings.xml

<resources>
<!--Case C-->
    <string name="cC1">Pemeriksaan kesihatan ke atas semua pengendali makanan:
    n - Mendapat suntikan pelalian anti-tifoid
    n - Menghadiri Latihan Pengendali Makanan </string>
    <string name="cC2">Tahap kebersihan diri yang baik:</string>
    <string name="cC2_1">- Berpakaian bersih dan bersesuaian</string>
    <string name="cC2_2">- Memakai apron yang bersih dan berpenutup kepala</string>
</resources>

styles.xml

<resources>

    <!--kpkt table header-->

    <style name="table_header">
        <item name="android:textSize">@dimen/textSize_subheading</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textColor">@color/table_textcolor</item>
        <item name="android:background">@color/table_background</item>
        <item name="android:padding">@dimen/text_padding</item>
        <item name="android:layout_width">0dp</item>
        <item name="android:gravity">center</item>
    </style>

    <!--table style-->

    <style name="table_row">
        <item name="android:gravity">end</item>
        <item name="android:layout_width">0dp</item>
        <item name="android:weightSum">5</item>
</style>

    <style name="table_attr">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_weight">0.3</item>
        <item name="android:gravity">center</item>
    </style>

    <style name="table_component">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_weight">2.5</item>
        <item name="android:paddingTop">8dp</item>
        <item name="android:paddingBottom">8dp</item>
    </style>

    <style name="table_marks">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_weight">1</item>
        <item name="android:gravity">center</item>
        <item name="android:padding">8dp</item>
    </style>

    <style name="table_demerit">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_weight">0.7</item>
        <item name="android:gravity">center_horizontal</item>
        <item name="android:visibility">visible</item>
    </style>

    <style name="table_notes">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_weight">0.5</item>
        <item name="srcCompat">@drawable/ic_edit</item>
        <item name="android:contentDescription">catatan</item>
        <!--<item name="android:drawableBottom">@drawable/ic_edit</item>-->
        <!--<item name="android:drawableTop">@drawable/ic_edit</item>-->
    </style>

    <!--kpkt table head component-->

    <style name="table_headComponent">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">40dp</item>
        <item name="android:padding">@dimen/text_padding</item>
        <item name="android:layout_weight">4.2</item>
    </style>

    <!--kpkt table subcomponent-->

    <style name="table_subDemerit">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_weight">1.2</item>
        <item name="android:visibility">visible</item>
    </style>

    <style name="table_subComponent">
        <item name="android:layout_width">0dp</item>
        <item name="android:layout_height">40dp</item>
        <item name="android:padding">8dp</item>
        <item name="android:layout_weight">2.5</item>
    </style>

</resources>

I can suggest you to understand the concept of wrap_content and match_parent, to get better solution.

I think you can use linear layout with horizontal and vertical orientation.

Because linear layout is more flexible and gives you better performance than table layout and table row.

Q: When we should use table layout and table row?

Ans: when we have a similar type of data, and we want to represent in rows and columns view then we should use table layout and table row.

But if we have non linear data, then we should not use table layout and table row...and I think you have non linear data.

For better understanding of showing data in non linear form, you should know the weight_sum and layout_weight property of linear layouts.

链接地址: http://www.djcxy.com/p/93250.html

上一篇: 编辑资源文件时,Android项目不会生成

下一篇: 布局时内容不包装