2016-09-29 9 views
3

RecyclerView 위에 titleText가 포함 된 선형 레이아웃이 포함 된 대화 상자 조각이 있는데, 맨 아래에는 recyclerView 아래에 버튼이 있습니다.RecyclerView의 최대 높이 설정

recyclerView는 어댑터가 설정하는 항목 수에 따라 확장되거나 축소되기 때문에 버튼이 잘 리거나 화면에 나타나지 않는 경우가 있습니다. 이는 recyclerView가 전체 화면을 포괄하기 때문입니다.

제 질문은 아래 버튼을 숨기지 않고 recyclerView의 최대 높이를 설정하는 방법입니다. 물론, 단순히 recyclerView에 항목이없는 경우를 대비해서보기에 임의의 높이를주고 싶지는 않습니다. 그리고 그것은 빈 섹션 일뿐입니다.

이 문제가 생겼다면 어떻게 해결했는지 알려주세요. 감사!

답변

4

업데이트 레이아웃 가중치를 사용하면 쉽게이를 수행 할 수 있습니다. 예를 들면 다음과 같습니다.

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

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <TextView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:gravity="center" 
      android:text="Title" 
      android:textSize="21sp"/> 

     <android.support.v7.widget.RecyclerView 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:paddingBottom="30dp"> 
     </android.support.v7.widget.RecyclerView> 

    </LinearLayout> 

    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom" 
     android:text="Submit"/> 
</FrameLayout> 

제목 및 RecyclerView는 내용에 따라 내용을 줄 것이고 버튼은 항상 아래쪽 부분을 차지합니다.

+0

이 솔루션은 layoutHeight 매개 변수를 설정하는 것과 본질적으로 비어있을 때 압축해야하는 뷰의 80 %를 recyclerView에 배포합니다. – jensiepoo

+0

업데이트 해 주셔서 감사합니다! 이것은 멋진 해결책입니다. 그러나 본질적으로이 버튼은 recyclerView 항목 위에 겹쳐서 표시됩니다. – jensiepoo

+0

재생기보기에 패딩 하단을 추가하기 만하면됩니다. –

0

본인의 경우와 같은 경우보기의 위치를 ​​처리하므로 실제로는 기본 디자인에 집중할 수 있으므로 RelativeLayout을 사용하는 것이 좋습니다.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:id="@+id/title" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:text="Some title" /> 
    <android.support.v7.widget.RecyclerView 
     android:id="@+id/recyclerView" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_below="@+id/title" 
     android:layout_above="@+id/button"/> 
    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_centerHorizontal="true" 
     android:gravity="center"/> 
</RelativeLayout> 

위의 XML 코드는 필요한 항목의 골격 코드입니다. 여백과 치수를 추가하여 간격을 제어 할 수 있습니다. 그러나 어떤 경우에도 (당신이 음의 마진을 제공 할 때까지) 당신의 견해는 서로 겹치지 않을 것입니다. layout_below 또는 안드로이드 : layout_above 또는 안드로이드 : layout_start 또는 안드로이드 : 완벽하게 보는 방법 당신 희망을 정렬 layout_end RelativeLayout의 사용의

홈페이지 트릭은 안드로이드와 같은 XML 태그를 사용하는 기능입니다.