2016-09-30 26 views
1

Card View包裝內容而不是匹配父項。所有列表項目都被包裝而不是匹配其寬度。 ScreenshotCardView是wrap_content列表項而不是match_parent

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

    <android.support.v7.widget.CardView 
     android:id="@+id/card_view" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_gravity="center" 
     android:layout_margin="@dimen/card_margin" 
     android:elevation="3dp" 
     card_view:cardCornerRadius="@dimen/card_album_radius"> 

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

      <ImageView 
       android:layout_width="80dp" 
       android:layout_height="80dp" 
       android:id="@+id/Album_Art" 
       android:layout_alignParentTop="true" 
       android:layout_alignParentLeft="true" 
       android:layout_alignParentStart="true" 
       android:src="@drawable/ic_google_music" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:text="@string/song_name" 
       android:id="@+id/Sname" 
       android:layout_alignParentTop="true" 
       android:layout_toRightOf="@+id/Album_Art" 
       android:layout_toEndOf="@+id/Album_Art" /> 

      <TextView 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:textAppearance="?android:attr/textAppearanceSmall" 
       android:text="@string/artist_album" 
       android:id="@+id/Sdata" 
       android:layout_centerVertical="true" 
       android:layout_centerHorizontal="true" 
       android:layout_toRightOf="@+id/Album_Art" 
       android:layout_toEndOf="@+id/Album_Art"/> 

     </RelativeLayout> 
    </android.support.v7.widget.CardView> 
</LinearLayout> 

手動設置寬度的作品。當我將它設置爲一個較大的dp值時,它工作正常,但列表項不在窗口中。我不明白爲什麼match_parent不起作用。任何導致如何解決這個問題將不勝感激。

+0

我猜想,你傳遞空父,當你吹你的'Adapter'佈局。 –

+0

@MikeM。是的,我必須通過什麼? –

+0

將'ViewGroup'傳遞給您在膨脹佈局的方法。可能命名爲「父」。 –

回答

2

你在使用recyclerView嗎? 如果是的話,你應該重寫這個在佈局管理器這樣的:

layoutManager = new LinearLayoutManager(getActivity()) { 
      @Override 
      public RecyclerView.LayoutParams generateDefaultLayoutParams() { 
       return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 
         ViewGroup.LayoutParams.WRAP_CONTENT); 
      } 
     }; 
    } 
相關問題