簡體   English   中英

如何將布局拆分為兩個相等的行?

[英]How to Split a Layout into two equal rows?

這是在主布局中編寫的代碼

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="vertical"
            >

    </LinearLayout>

我需要在這個父布局中有兩個線性布局,這樣父布局就會分成兩半。

使用此代碼

 <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="100dp"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

                //Other Stuff

            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

                //Other Stuff

            </LinearLayout>

        </LinearLayout>

您可以使用以下代碼,

<?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="100dp"
    android:weightSum="2"
    android:orientation="vertical">

    <LinearLayout
        android:background="@android:color/black"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <!--child 1-->

    </LinearLayout>

    <LinearLayout
        android:background="@android:color/holo_blue_dark"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <!--child 2-->

    </LinearLayout>

</LinearLayout>

我們已經使用 layout_weight 來創建這些類型的布局。 試試這個代碼:

<?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="100dp"
    android:weightSum="1"
    android:orientation="vertical">
    <!--First Row-->
    <LinearLayout
        android:background="@android:color/black"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".5">



    </LinearLayout>
<!--Second Row-->
    <LinearLayout
        android:background="@android:color/holo_blue_dark"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight=".5">



    </LinearLayout>

</LinearLayout>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM