0

このレイアウト定義では:

<?xml version="1.0" encoding="utf-8"?>

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

    <LinearLayout
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="7"
            android:id="topLayout"
            android:background="@android:color/holo_green_light">

    </LinearLayout>

    <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal"
            android:layout_weight="2"
            android:id="bottomLayout"
            android:background="@android:color/holo_orange_light">

    </LinearLayout>
</LinearLayout>

名前付きの「bottomLayout」がtopLayoutよりも高い理由がわかりません。Android Studio で、コメント付きの結果のスクリーンショットを確認できます。

4

4 に答える 4

1

まず、xml を修正し、layout_height を 0dp に変更します。これは、重みを使用して高さを管理し、同時に親を埋めるように指示しているためです。次に、重みをそれぞれ 1 として実験すると、両方のレイアウトが均等に分割されていることがわかります。重みは、ビューを追加した後に残っている使用可能なスペースの計算です。つまり、重みが計算されます。利用可能なスペースに応じて。

レイアウトが画面からはみ出していないか、アウトラインをクリックしてプレビューを確認してください。レイアウトの一部が画面からはみ出している場合があります。明確にするために、たとえば、2 と 7 を与える代わりにパーセンテージに従ってウェイトを使用し、0.2 と 0.8 で試してください。これにより、ウェイトのバランスが取れます。または、属性「weight_sum」を使用して利用可能な総重量を宣言し、それを均等に配分できます。たとえば、weight_sum 100 を使用すると、パーセンテージ ベースのアプローチに従うことができます。

さらに明確にするために、このリンクを参照してください。

于 2013-08-09T11:03:16.147 に答える
1

LinearLayout子は、宣言された順に配置されます。

layout_weightメカニズムは、要素の重みに比例して残りのスペースを要素に分配するだけで、順序付けには影響しません。

これは、「重量」パラメーターがコンテナー内のアイテムの位置に影響を与える他の環境とは異なります。

于 2013-08-09T11:05:15.223 に答える
0

linearlayout で layout_weight を使用する場合は、親 LinearLayout に weightSum を追加する必要があります

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        アンドロイド: 重量合計 = "10"
        android:orientation="垂直"
    >

        <--親 LinearLayout の空き領域の 70%-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            アンドロイド:layout_weight="7"
        >
        </LinearLayout>
        <--親 LinearLayout の空き容量の 30%-->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            アンドロイド:layout_weight="3"
        >
        </LinearLayout>

    </LinearLayout>

xml コメントで、正確な高さのレイアウトを追加すると、親LinearLayout の空き領域の 70 % を書き込みました。たとえば、親の LinearLayout の高さが 100 dp の場合、両方の linearlayout がその特定の linearlayout の左の高さの 70% と 30% を占めます。

子レイアウトは最初に70dpで描画され、2番目のレイアウトは30dpの高さになりますが、高さ50dpのイメージビューを追加すると、最初の子のlinearlayoutは高さが約35dp、2番目のレイアウトは15dpになります

于 2013-08-09T11:00:42.637 に答える
0

このようにコードを作成すると、解決策を見つけることができます

    <LinearLayout
        android:![enter image description here][1]orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="7"
        android:id="topLayout"
        android:background="@android:color/holo_green_light">

</LinearLayout>

<LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_weight="3"
        android:id="bottomLayout">

    <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="b"
            android:id="@+id/button4"
            android:layout_gravity="center"
            android:background="@android:color/holo_orange_light"/>

</LinearLayout>

于 2013-08-09T10:51:13.113 に答える