2011-05-11 7 views
4

すべてのサイズのデバイスに合わせてlayout_weightを使用してアンドロイドレイアウトを作成しようとしています。Android layout_weight comportment

layout_width/layout_heightを変更すると、layout_weightに影響していましたが、わかりません。

たとえば、垂直のLinearLayoutを3つの内側のLinearLayoutに分割して、上部の1つと下部の1つが画面の25%を満たし、中央の50%がオンになるようにしたいとします。それは内部のレイアウトの内容に依存すべきではありません。 (インナーLinearLayoutの内容が大きすぎる場合、それは他のレイアウトをシフトべきではありません)

これを行うために、私はfill_parentまたはwrap_contentにインナーLinearLayoutlayout_height属性を設定する必要がありますか?

ありがとうございます!

EDIT:layout_weightは、レイアウトが占めるサイズに反比例するようです。

3例:

重量1/1/1(私が期待どおりに動作し)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
     <LinearLayout 
      android:id="@+id/layout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#FF0000"/> //RED 
     <LinearLayout 
      android:id="@+id/layout2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#00FF00"/> //GREEN 
     <LinearLayout 
      android:id="@+id/layout3" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#0000FF"/> //BLUE 
</LinearLayout> 

結果: enter image description here

重量1/2/1(なぜ、ああ、なぜ?)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
     <LinearLayout 
      android:id="@+id/layout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#FF0000"/> //RED 
     <LinearLayout 
      android:id="@+id/layout2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="2" 
      android:background="#00FF00"/> //GREEN 
     <LinearLayout 
      android:id="@+id/layout3" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" 
      android:background="#0000FF"/> //BLUE 
</LinearLayout> 

結果: enter image description here

**重量3/2/3(私は1/2/1をどうするentended何):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/mainLayout" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 
     <LinearLayout 
      android:id="@+id/layout1" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="3" 
      android:background="#FF0000"/> //RED 
     <LinearLayout 
      android:id="@+id/layout2" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="2" 
      android:background="#00FF00"/> //GREEN 
     <LinearLayout 
      android:id="@+id/layout3" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="3" 
      android:background="#0000FF"/> //BLUE 
</LinearLayout> 

結果:あなたが設定する必要がありenter image description here

答えて

1

3つの内部レイアウトすべてのlayout_heightを "fill_parent"に変更してから、それらの重みを変更して、必要なように見せます。あなたが期待される(および文書化)としてlayout_weightが機能するためにはlayout_height = 0dp

+0

@Egor:これを実行しようとしましたが、この場合layout_weightがどのように動作するのか分かりません。1/1/1を入力すると、3つの部分が等しくなります。しかし、私が1/2/1を置くと、私は2番目のものを見ません。そして、スクリーンはちょうど最初のレイアウトの50%と3番目のレイアウトの50%で分割されます。どのように機能するのですか? – nbarraille

+0

@nbarraille:スペースを指定して配布する必要があるので、問題のレイアウトを投稿して、その特定のケースで何が問題かをよりよく評価できるようにする必要があります。 – dmon

+0

@dmon:最初の投稿を例文で更新しました。 – nbarraille

3

あなたはfill_parentmatch_parentどちらにlayout_heightを設定してはいけません。

http://developer.android.com/guide/topics/ui/layout/linear.html#Weightの右側の例は、layout_height0dpに設定されている例を示しています。ただし、まったく設定しないか、wrap_contentに設定しても、layout_weightが正常に動作します。

3

を設定しようとすることができます

+0

時間を費やした後、このソリューションは機能しました! – Massimo

関連する問題