2016-04-01 4 views
0

私はweightSumとLayoutWidthを理解することに問題があるようです。私のコードは以下の通りです:アンドロイドでweightSumでレイアウトを分割する方法は?

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

    <LinearLayout 

     android:background="#cccccc" 
     android:layout_weight="1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <TextView 

      android:padding="20sp" 
      android:textSize="30sp" 
      android:gravity="center_horizontal" 
      android:text="Cheapest Fare Option" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 


    </LinearLayout> 

    <LinearLayout 

     android:background="#666666" 
     android:layout_weight="2" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
    </LinearLayout> 



</LinearLayout> 

本の私の理解では、スペースの2/3がかかります第一レイアウトはスペースと第二のレイアウトの1/3を取るだろうが、その逆が起こっている、すなわち第一レイアウトです2/3をとり、2番目のレイアウトは1/3になります

どうしてですか?これを理解しようと努力しています。あなたのandroid:orientation="vertical"を取る

答えて

0

、あなたは垂直ので、あなたのandroid:layout_height属性が子供LinearLayout両方のための0dpする必要があり、この比率を取得したいです。

セット - インナーLinearLayout両方のための

android:layout_height="0dp" 

。両方の内側のLinearLayoutため

android:layout_width="0dp" 

- あなたは比率を取得する場合

代わりに水平に、その後android:orientation="horizontal"セットを使用します。

あなたが比率を達成したい場合、その特定の寸法(幅または高さ)を親に合わせるように設定しないことです。 Androidがあなたのために自動的に処理できるように、それを0dpに設定します。

+0

素晴らしい、ありがとう! upvoteを表示するには十分な評判がない! – zooter

0

<LinearLayout 
    android:background="#cccccc" 
    android:layout_weight="1" 
    android:layout_width="match_parent" 
    android:layout_height="0dp"> 

    <TextView 
     android:padding="20sp" 
     android:textSize="30sp" 
     android:gravity="center_horizontal" 
     android:text="Cheapest Fare Option" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 


</LinearLayout> 

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

関連する問題