2016-07-19 10 views
2

子ビューグループがアイテムの高さに一致するようなRecyclerViewアイテムレイアウトを作成しようとしています。例えば、250dpの高さのアイテムがあり、 match_parentに設定すると、高さも250dpになります。子ビューグループを親の高さに一致させるRecyclerView

これは私がこれを行うにしようとすると、ケースのように思われない、のLinearLayoutの高さは、これは私が

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

    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="250dp" 
      android:layout_toLeftOf="@+id/ll" 
      android:background="@color/android_green"> 

     </RelativeLayout> 

     <LinearLayout 
      android:layout_width="175dp" 
      android:layout_height="match_parent" 
      android:orientation="horizontal" 
      android:id="@+id/ll" 
      android:layout_alignParentRight="true" 
      android:background="@color/android_red"> 

     </LinearLayout> 
    </RelativeLayout> 
</RelativeLayout> 

をテストするために使用しています私のレイアウト例であるwrap_content

に強制しているようですハード・ハイト(250dp)を設定するとLinearLayoutが表示されますが、match_parentに変更すると消えます。

これはRecyclerViewでもう使用できませんか?

+0

することができますあなたが直面している問題をもう少し詳しく教えてください。 –

+0

@subrahmanyamboyapati線形レイアウトは、セルの高さに一致するモーションです。常にコンテンツの高さです。 – tyczj

+0

「RelativeLayout」は、特に入れ子になっているときに痛みがあります。 'LinearLayout'の' layout_alignParentTop'属性と 'layout_alignParentBottom'属性を両方とも' true'に設定してみてください。 –

答えて

0

あなたはそれが250dpであると予想していますか?つまり、LinearLayoutは2番目のRelativeLayoutの内側にあります。 match_parentを設定すると、ハードコードされた250dpの3rd Order RelativeLayoutではなく、そこから高さをコピーするようになります。

あなたのアプリがすべてのデバイスで動作できるように、それを維持してJavaでスケーリングすることをお勧めします...ハードコーディングの数字は実際には最善のことではありません...しかし、DOに正確に250dpであれば、LinearLayoutを3次のRealativeLayoutの子にします。

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

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

     <RelativeLayout 
      android:layout_width="match_parent" 
      android:layout_height="250dp" 
      android:layout_toLeftOf="@+id/ll" 
      android:background="@color/android_green"> 

      <LinearLayout 
       android:layout_width="175dp" 
       android:layout_height="match_parent" 
       android:orientation="horizontal" 
       android:id="@+id/ll" 
       android:layout_alignParentRight="true" 
       android:background="@color/android_red"> 
      </LinearLayout> 

     </RelativeLayout> 
    </RelativeLayout> 
</RelativeLayout> 

UPDATE

私は完全に2次RelativeLayoutの削除についてマイク・M.さんのコメントに同意します。 (少なくとも提供されたコードではない)明らかに目的を果たしておらず、無駄にプログラムを複雑にしています。あなたがそれを必要とするならば、おそらくそれほど乱雑ではない別のレイアウトを探してみてください。

0

あなたが直面している問題について説明できますか?以下の問題がある場合は、以下のように変更してください。

親レイアウト(LinearLayoutの親)、つまり相対レイアウトの高さを250dpに変更してください。今はmatch_parentなので、レイアウトサイズは250dpと同じになります。 がここに以下の変更されたコードを入れて、あなたの意図はそれのようで、その後することができ、子ビュー・グループマッチ親の高さを作るためのアプローチの下に使用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content"> 

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="250dp"> 
    <RelativeLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_toLeftOf="@+id/ll" 
     android:background="@color/color_dark_yellow"> 

    </RelativeLayout> 

    <LinearLayout 
     android:layout_width="175dp" 
     android:layout_height="fill_parent" 
     android:orientation="horizontal" 
     android:id="@+id/ll" 
     android:layout_alignParentRight="true" 
     android:background="@color/color_green_txt"> 

    </LinearLayout> 
</RelativeLayout> 

0

のように変更します。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <RelativeLayout 

     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <RelativeLayout 
      android:id="@+id/rl_child" 
      android:layout_width="match_parent" 
      android:layout_height="250dp" 
      android:layout_toLeftOf="@+id/ll" 
      android:background="#00FF00"> 

     </RelativeLayout> 

     <RelativeLayout 
      android:id="@+id/ll" 
      android:layout_width="175dp" 
      android:layout_height="match_parent" 
      android:layout_alignBottom="@+id/rl_child" 
      android:layout_alignParentRight="true" 
      android:background="#FF0000" 
      android:orientation="horizontal"> 

     </RelativeLayout> 
    </RelativeLayout> 
</RelativeLayout> 
関連する問題