2017-12-27 16 views
-1

ちょっと、2つのRelativeLayoutを使ってLinearLayoutを作成しました。 1つのRelativeLayoutのウェイトは8、もう1つのウェイトは2です。私がアプリケーションを起動すると、画面の80%を使用するレイアウトは画面の20%を占め、20%の画面を使用する他のRelativeLayoutは80スクリーンの%。LinearLayout Weightが正しく動作しない

ここに私のコード:

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

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@color/colorPrimary" 
    android:layout_weight="8"> 
</RelativeLayout> 

<RelativeLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="#FFFFFF" 
    android:layout_weight="2"> 
</RelativeLayout> 
</LinearLayout> 
+0

正しく動作させるには、正しく動作させる必要があります。加重ディメンションのサイズは、** 0dp **でなければなりません。それでおしまい。 –

答えて

0

のLinearLayoutの重みを使用している場合、あなたはweightSum値の世話をする必要があります。デフォルトでは1で、layout_weightでは浮動小数点値を必要に応じて使用できます。子供はlayout_weightの値を指定し、親の向きに応じてlayout_widthまたはlayout_height0dpでなければなりません。 Android Studioが自動的にあなたに警告するので、自分で修正していないのは少し奇妙です。とにかく、あなたの下には固定レイアウトがあります。それがあなたのために働くかどうか私に教えてください。

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

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:background="@color/colorPrimary" 
     android:layout_weight="8"> 
    </RelativeLayout> 

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:background="#FFFFFF" 
     android:layout_weight="2"> 
    </RelativeLayout> 

</LinearLayout> 

また、あなたがそれを使用した場合、それはあなたがAPIレベル8+のために開発されることを意味するので、あなたは「match_parent」を使用する必要がありますので、私は(それが交換された、match_parentfill_parentを交換しますが、値は同じまま"-1")。

+1

'weightSum'は必要ありません。 –

+0

この場合、あなたは正しいです:) – FonzTech

+1

ああ、私はちょうどRelativeLayoutのlayout_heightを修正していただきありがとうございます。 –

関連する問題