Android Layout - layoutweight and weightsum

白昼怎懂夜的黑 提交于 2019-11-27 14:26:16

Use this XML file. I have made the changes for you.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/background"
android:orientation="vertical"
android:weightSum="100" >

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="91"
    android:orientation="horizontal"
    android:weightSum="100" >

    <!-- Below is the first modification to layout_width -->

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="40"
        android:orientation="vertical"
        android:weightSum="235" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="100"
            android:background="#ff0000"
            android:gravity="center"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000000" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="45"
            android:background="#ffff00"
            android:orientation="vertical" >

            <ViewFlipper
                android:id="@+id/view_flipper"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_below="@+id/tvItemName" >

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical" >
                </LinearLayout>
            </ViewFlipper>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="90"
            android:background="#ffffff"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>

    <!-- Below is the second modification to layout_width -->

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="60"
        android:orientation="vertical"
        android:weightSum="100" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="30"
            android:background="#00ab00"
            android:orientation="vertical"
            android:weightSum="100" >

            <TextView
                android:id="@+id/textView1"
                android:layout_width="match_parent"
                android:layout_height="fill_parent"
                android:text="Test data Test dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest dataTest data"
                android:textColor="#000000" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="70"
            android:background="#cd00ab"
            android:orientation="vertical" >
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="9"
    android:background="#ab0000" >
</LinearLayout>

</LinearLayout>

It looks fine to me. check out the below snapshot.

Start your layout_width or layout_height as 0dp instead of wrap_content. (If it's in a vertical layout, layout_height should be 0dp; if it's in a horizontal layout, layout_width should be 0dp)

The layout_weight describes how to divide the remaining space after the layout_widths/layout_heights have been assigned.

Note that wrap_content doesn't mean "make the text wrap"; it means set that dimension to the "preferred" dimension of the view.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!