定义布局的宽度和高度 android

defining width and height to layouts android

我的要求是在一个布局中做10个布局。每个布局应为 50% 水平和 20% 垂直。

目前我的代码可以使水平 50%,但如何使相同的布局垂直 20%。

能不能。

代码是

<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:orientation="horizontal">

<LinearLayout android:layout_width="0dip"
    android:layout_height="wrap_content" android:orientation="horizontal"
    android:id="@+id/linearLayoutouter" android:layout_weight=".5"
    android:background="@android:color/holo_green_dark">

    <Button android:text="Button" android:id="@+id/button11"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_gravity="center">
    </Button>

</LinearLayout>

<LinearLayout android:layout_height="wrap_content"
    android:id="@+id/linearLayout1" 
    android:orientation="vertical"
    android:layout_width="0dip" 
    android:layout_weight=".5"
    android:background="@android:color/holo_orange_light">
    <Button android:text="Button" 
        android:id="@+id/button1"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_gravity="center">
    </Button>
    <Button android:layout_width="wrap_content" 
        android:id="@+id/button2"
        android:layout_height="wrap_content" 
        android:text="Button"
        android:layout_gravity="center"></Button>

</LinearLayout>

您可以使用嵌套布局来做到这一点,例如网格。

<!-- Row 0 -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:orientation="vertical">
    <!-- Row 0: Column 0-->
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".5"
        android:orientation="horizontal">
        <Button ... />

    </LinearLayout>
    <!-- Row 0: Column 1-->
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight=".5"
        android:orientation="horizontal">
        <Button ... />

    </LinearLayout>
</LinearLayout>
...

...
<!-- Row 4 -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"
    android:orientation="vertical">
    <!-- Row 4: Column 0-->
    ...
</LinearLayout>

看起来有点奇怪,也许你可以考虑使用 ListView 或其他更适合的策略。

您可以像下面这样使用 weightSum 属性

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="5">

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:weightSum="2"
        android:layout_height="0dp"
        android:layout_weight="1">

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

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

    <LinearLayout
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:weightSum="2"
        android:layout_height="0dp"
        android:layout_weight="1">

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

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

    <!-- 3 more rows here -->

</LinearLayout>

外部 weightSum 用于 5 行。内部 weightSum 用于 2 列

您应该在您的线性布局中添加另一个子布局以避免嵌套权重。 并检查这个 What is android:weightSum in android, and how does it work?

试试这个..希望对你有用..

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/linearLayoutouter"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight=".5"
        android:background="#ff0000"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/button11"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Button" >
        </Button>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight=".5"
        android:background="#000000"
        android:orientation="vertical" >

        <LinearLayout
            android:id="@+id/linearLayout2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:orientation="vertical"
            android:weightSum="10" >

            <Button
                android:id="@+id/button1"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <Button
                android:id="@+id/button2"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <Button
                android:id="@+id/button3"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <Button
                android:id="@+id/button4"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <Button
                android:id="@+id/button5"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <Button
                android:id="@+id/button6"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <Button
                android:id="@+id/button7"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <Button
                android:id="@+id/button8"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <Button
                android:id="@+id/button9"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <Button
                android:id="@+id/button10"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_gravity="center"
                android:layout_weight="1"
                android:text="Button" >
            </Button>

            <!-- added 8 mote view with weight 1 -->
        </LinearLayout>
    </LinearLayout>

</LinearLayout>