2016-11-19 8 views
-1

私はアンドロイドのレイアウトをよりよく理解しようとしています。次の例で私は問題を解決しましたが、試行錯誤によってのみ解決しました。私はそれが働いた理由を理解したいと思います。アンドロイドレイアウトの重さと重力の問題

私は別のレイアウトの中に「ヘッダー」行を設定しています。最初の部分はできるだけ多くの幅を使い、2番目の部分は必要な部分だけを使うようにします。だから私はlayout_1の重みを1、layout_2の重みを0にするように設定しました。もともとは、layout_widthにmatch_parentの両方を持っていました。その結果、layout_2は幅全体を占め、layout_1は消滅しました。最終的にlayout_2の幅をwrap_contentに設定して修正しました。 layout_2が幅wrap_contentを持つことは意味があると私は理解しています。しかし、なぜlayout_2 match_parentが0の重みを持ち、layout_1の幅がmatch_parentであるときにmatch_2parentが幅全体をとるのか理解していない。

コードの例を次に示します。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_height="match_parent" 
      android:layout_width="match_parent"> 

<LinearLayout android:id="@+id/header_layout_1" 
       android:orientation="horizontal" 
       android:layout_height="wrap_content" 
       android:layout_width="match_parent" 
       android:layout_gravity="center" > 

    <LinearLayout android:orientation="horizontal" 
        android:layout_height="match_parent" 
        android:layout_width="match_parent" 
        android:gravity="center" 
        android:layout_weight="1"> 
     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="some text"/> 
    </LinearLayout> 

    <!-- changing width on header_layout_2 to match_parent takes over layout, wrap_content gives me what I want --> 
    <LinearLayout android:id="@+id/header_layout_2" 
      android:orientation="horizontal" 
        android:layout_height="match_parent" 
        android:layout_width="match_parent" 
        android:layout_weight="0"> 
     <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="some more text"/> 
    </LinearLayout> 
</LinearLayout> 
<!--end of header--> 
</LinearLayout> 

答えて

0

あなたはインターネットについてのLinearLayoutlayout_weight上のソースやブログの多くを見つけることができます。

いくつかの有用なウェブサイトがあります:

https://ugia.io/2012/01/19/android-linearlayout-distribution-explained-weight-and-sizes/

http://www.101apps.co.za/index.php/articles/using-android-s-layout-weight-attribute.html

https://blog.stylingandroid.com/layout-weights-part-1/

https://www.mkyong.com/android/android-linearlayout-example/