卡片视图将内容包装而不是匹配父布局。 所有列表项都包裹在内,而不是匹配其宽度。
<?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 不起作用。如果有任何解决方法,请告诉我,谢谢。
Adapter
中膨胀布局时,将父级参数传递为null。 - Mike M.ViewGroup
。可能被命名为parent
。 - Mike M.TextView
设置为match_parent
。 - Mykhailo