CardView 是 wrap_content 列表项而不是 match_parent
CardView is wrap_content list item instead of 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 值时,它工作正常,除了列表项在 window 之外。我不明白为什么 match_parent 不起作用。任何有关如何解决此问题的线索都将不胜感激。
你在使用 recyclerView 吗?
如果是这样,您应该像这样在布局管理器上覆盖它:
layoutManager = new LinearLayoutManager(getActivity()) {
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
};
}
如果您使用的是 RecyclerViewAdapter
,请确保您使用的是 onCreateViewHolder
中的第一个 inflate 方法签名而不是第二个签名。
1. inflate(LayoutInflater inflater, ViewGroup root, boolean attachToRoot)
2. inflate(LayoutInflater inflater)
并且,一定要为 root 传递 parent,为 attachToRoot 传递 true/false。
卡片视图包装内容而不是匹配父项。所有列表项都被换行而不是匹配其宽度。
<?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 值时,它工作正常,除了列表项在 window 之外。我不明白为什么 match_parent 不起作用。任何有关如何解决此问题的线索都将不胜感激。
你在使用 recyclerView 吗? 如果是这样,您应该像这样在布局管理器上覆盖它:
layoutManager = new LinearLayoutManager(getActivity()) {
@Override
public RecyclerView.LayoutParams generateDefaultLayoutParams() {
return new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
}
};
}
如果您使用的是 RecyclerViewAdapter
,请确保您使用的是 onCreateViewHolder
中的第一个 inflate 方法签名而不是第二个签名。
1. inflate(LayoutInflater inflater, ViewGroup root, boolean attachToRoot)
2. inflate(LayoutInflater inflater)
并且,一定要为 root 传递 parent,为 attachToRoot 传递 true/false。