2

垂直レイアウト内に3つの水平レイアウトがあります。これらの3つの水平方向の子レイアウトが同じスペースを占めるようにしたいので、それぞれに重み(0.33)を追加します。

親レイアウトは垂直ですが、子レイアウトが期待どおりに同じ高さを占めていないため、3つの子レイアウトコンポーネントのレイアウト高さを0dpに割り当てています。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/paper"
    android:paddingLeft="4dp"
    android:paddingRight="8dp"
    tools:context=".TestActivity" >

         <LinearLayout
            android:id="@+id/scratchLayout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="vertical" >

             <LinearLayout
                android:id="@+id/topLayout"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="0.33"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp" 
                android:orientation="horizontal" >

                <TextView
                    android:id="@+id/topArea1"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@android:color/transparent"
                    android:paddingRight="3dp"
                    android:text="Top Area 2"
                    android:textSize="15dp" />

                <TextView
                    android:id="@+id/topArea2"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="@android:color/transparent"
                    android:paddingRight="3dp"
                    android:text="Top Area 1"
                    android:textSize="15dp" />
             </LinearLayout>

             <LinearLayout
                android:id="@+id/centerLayout"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="0.33"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp" 
                android:orientation="horizontal" >

                 <TextView
                 android:id="@+id/centerArea1"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:layout_marginRight="3dp"
                 android:text="Center Area 1" />

                 <TextView
                 android:id="@+id/centerArea2"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:layout_marginRight="3dp"
                 android:text="Center Area 2" />

            </LinearLayout>

             <LinearLayout
                android:id="@+id/BottomLayout"
                android:layout_width="wrap_content"
                android:layout_height="0dp"
                android:layout_weight="0.33"
                android:layout_marginLeft="3dp"
                android:layout_marginRight="3dp" 
                android:orientation="horizontal" >

                 <TextView
                 android:id="@+id/bottomArea1"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:layout_marginRight="3dp"
                 android:text="Bottom Area 1" />

                 <TextView
                 android:id="@+id/bottomArea2"
                 android:layout_width="fill_parent"
                 android:layout_height="wrap_content"
                 android:layout_marginRight="3dp"
                 android:text="Bottom Area 2" />

            </LinearLayout>

         </LinearLayout>

</RelativeLayout>
4

2 に答える 2

3

クイック ノート: ネストされたウェイトはパフォーマンスの低下につながります。可能であれば避けてください。match_parentとを混ぜないでくださいfill_parent。を使用するだけmatch_parentです。

あなたの主な問題は、あなたがlayout_height="wrap_content"あなたのアウターのために持っているということですがLinearLayout、それからあなたには重みがあります。各子レイアウトを合計の 1/3 に指定していますが、合計はその子の合計になるはずです。各子レイアウトは 1 dp にする必要がありますか? 100dp? それぞれを画面全体の 1/3 にしたい場合はlayout_height="match_parent"、outer に設定する必要がありますLinearLayout

于 2013-02-26T01:16:02.340 に答える
0

Kabuko に同意しますが、fill_parent をすべて match_parent に変更してください。

前述のように高さを 0dp に変更することをお勧めしますが、すべての LinearLayout の重みを 1 に設定します。

重量値は、パーセンテージではなく「係数」です。

これを参照してくださいAndroid:layout_weight とはどういう意味ですか? layout_weight の使用方法の明確な例について

于 2013-02-26T01:22:33.250 に答える