2016-07-15 156 views
0

我正在關注android編程教程,並遇到了recycleviews問題。視圖中的每個單元格似乎都會填充整個空間,而不僅僅是單元格中的信息。如下圖所示:RecycleView單元填充屏幕

enter image description here

activity_fragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:context="com.example.moore.criminalintent.CrimeActivity"> 


    <include layout="@layout/content_fragment" /> 


</android.support.design.widget.CoordinatorLayout> 

content_fragment.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    app:layout_behavior="@string/appbar_scrolling_view_behavior" 
    tools:context="com.example.moore.criminalintent.CrimeActivity" 
    tools:showIn="@layout/activity_fragment"> 

    <FrameLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/fragment_container" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"></FrameLayout> 
</RelativeLayout> 

fragment_crime.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/Crime_title_label" 
     style="?android:listSeparatorTextViewStyle"/> 
    <EditText 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/crime_title" 
     android:hint="@string/crime_title_hint"/> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/crime_detail_label" 
     style="?android:listSeparatorTextViewStyle"/> 
    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/crime_date" 
     android:layout_marginLeft="16dp" 
     android:layout_marginRight="16dp"/> 
    <CheckBox 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/crime_solved" 
     android:layout_marginLeft="16dp" 
     android:layout_marginRight="16dp" 
     android:text="@string/crime_solved_label"/> 
</LinearLayout> 

fragment_cr ime_list.xml

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.v7.widget.RecyclerView 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/crime_recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"/> 

list_item_crime.xml 

<?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:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/list_item_crime_solved_check_box" 
     android:layout_alignParentRight="true" 
     android:padding="4dp"/> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/list_item_crime_title_text_view" 
     android:layout_toLeftOf="@id/list_item_crime_solved_check_box" 
     android:textStyle="bold" 
     android:padding="4dp" 
     tools:text="Crime Title"/> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/list_item_crime_date_text_view" 
     android:layout_toLeftOf="@id/list_item_crime_solved_check_box" 
     android:layout_below="@id/list_item_crime_title_text_view" 
     android:padding="4dp" 
     tools:text="Crime Date"/> 

</RelativeLayout> 

我可以發佈更多的代碼,如果需要的話。任何有關如何解決這個問題的幫助將不勝感激。

+0

在LIST_ITEM crime.xml使相對佈局高度WRAP_CONTENT。 –

回答

0

試試這個:

list_item_crime.xml

<?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:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <CheckBox 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/list_item_crime_solved_check_box" 
     android:layout_alignParentRight="true" 
     android:padding="4dp"/> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/list_item_crime_title_text_view" 
     android:layout_toLeftOf="@id/list_item_crime_solved_check_box" 
     android:textStyle="bold" 
     android:padding="4dp" 
     tools:text="Crime Title"/> 
    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/list_item_crime_date_text_view" 
     android:layout_toLeftOf="@id/list_item_crime_solved_check_box" 
     android:layout_below="@id/list_item_crime_title_text_view" 
     android:padding="4dp" 
     tools:text="Crime Date"/> 

</RelativeLayout> 
+0

這是正確的感謝。這樣愚蠢的錯誤。 – user2320239

+0

很高興幫助! –

+0

@ user2320239如果這個答案對你有幫助,請接受它爲未來的訪問者。 –