CardView的高度在单独应用程序中测试正常,但是当使用相同的CardView代码来创建RecyclerView列表项时,高度不再显示。
这是RecyclerView列表项的代码:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_height="wrap_content"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_margin="10dp">
<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
card_view:cardUseCompatPadding="true"
app:cardMaxElevation="6dp"
card_view:cardCornerRadius="3dp"
card_view:cardElevation="24dp">
<LinearLayout android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/thumbnail"
android:layout_width="match_parent"
android:layout_height="250dp" />
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/thumbnail"
/>
</LinearLayout>
</android.support.v7.widget.CardView>
这是我的主要活动xml文件:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/bitmap"
android:layerType="software"
android:padding="10dp"
tools:context="com.example.android.vikramadityastimeline.MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/card_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
/>
</FrameLayout>
onBindViewHolder()
内更改cardView
的背景,那么它的阴影就不会显示。 - MSS