2016-05-23 4 views
0

最初の子をLinearLayoutの高さをmatch_parentに設定すると、画面のサイズに高さが吹きます。なぜこれが起こっているのか分かりません。親はRelativeLayoutなので、最初の子の高さをとし、その高さがmatch_parentに設定されている場合は、RelativeLayoutの高さはどうでもいいですか?子ビューの高さをmatch_parentに設定したときに、間違った親ビューを使用する場合

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="10dp" 
    android:layout_marginBottom="10dp" 
    android:background="@null"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginStart="6dp" 
     android:layout_toStartOf="@+id/vote_container" 
     android:gravity="center"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:background="@null" 
      android:text="1M"/> 

    </LinearLayout> 

    <LinearLayout 
     android:id="@id/vote_container" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     android:layout_centerHorizontal="true" 
     android:gravity="center_horizontal"> 

     <ImageButton 
      android:id="@+id/upvote_button" 
      android:layout_width="30dp" 
      android:layout_height="17dp" 
      android:layout_centerHorizontal="true" 
      android:background="@null" 
      android:src="@drawable/ic_keyboard_arrow_up"/> 

     <TextView 
      android:id="@+id/number_of_votes" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerHorizontal="true" 
      android:layout_below="@id/upvote_button" 
      android:hint="100"/> 

     <ImageButton 
      android:id="@+id/downvote_button" 
      android:layout_width="30dp" 
      android:layout_height="17dp" 
      android:layout_centerHorizontal="true" 
      android:layout_below="@id/number_of_votes" 
      android:background="@null" 
      android:src="@drawable/ic_keyboard_arrow_down"/> 

    </LinearLayout> 

</RelativeLayout> 

答えて

1
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

したいということですか?

+0

もし私がrelativelayoutの高さをmatch_parentにしたいのであれば、それは –

1

これは必ずしも問題の解決策ではありませんが、回避策の方が多くあります。最初のLinearLayoutの上部と下部を2番目のLinearLayoutの - @ id/vote_containerに合わせるだけです。最初のLinearLayoutの高さが2番目のLinearLayoutの高さより大きくならないようにすると、これは問題ではありません。もしそうなら、私は2つの間の制約関係を逆転することができます。

RelativeLayoutの高さがwrap_contentに設定されており、LinearLayout最初の高さはmatch_parentに設定されているため、問題のコードが動作しない理由は、親の高さに高さを計算するためにそう子が決定されますが、最初の子の高さを計算するために、親の高さが決定されます。つまり、再帰関係が作成され、Androidはそれを直感的に処理しません。

この場合、親の階層は、wrap_content以外の高さのものが見つかるまで検索され、その親の値は最初のLinearLayoutの高さに使用されます。私の場合、RelativeLayout親にもう1つの親があり、それはLinearLayoutで、高さもwrap_contentに設定されているため、wrap_content以外の高さの値を持つ唯一の親はスーパービューでした。置かれた。そのビューの高さは単純に画面のサイズです。

これは私のLinearLayoutの高さが画面のサイズまで吹き飛んでいたからです。

+0

です。これは、2つのLinearLayouts(垂直)を入れ子にしたLinearLayout(水平)を持つTableRowに関連したやや似た問題に役立ちました。私は、TableRowが既に水平なLinearLayoutであり、正しい親を見つけたので、hozontal LinearLayoutを取り出しました。 – jwehrle

関連する問題