【发布时间】:2016-07-04 13:23:57
【问题描述】:
RecyclerView 中的 CardView 有问题。
我的CardView 突然变成黑色,我的RatingBar 变成蓝色。我正在使用InfiniteRecyclerView,但将其更改为简单的RecyclerView 无效。我可以将CardView 的背景颜色更改为白色,但RatingBar 仍然是蓝色的。这是正在发生的事情的一个例子:
我在具有相同适配器的片段内的另一个活动中使用正常的RecyclerView,它看起来很好。这是一个例子:
这是single_recipe_recycler_item.xml 布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:orientation="horizontal">
<ImageView
android:id="@+id/recipe_recycler_image"
android:layout_width="80dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:src="@mipmap/ic_launcher" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp">
<!-- RealmRecipe Title -->
<TextView
android:id="@+id/recipe_recycler_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:gravity="end"
android:text="Very very very very very very long title"
android:textColor="@color/colorBlack"
android:textSize="@dimen/title"
android:textStyle="bold" />
<!-- Publisher -->
<TextView
android:id="@+id/recipe_recycler_publisher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@id/recipe_recycler_title"
android:layout_marginTop="5dp"
android:text="Publisher"
android:textColor="@color/colorBlack"
android:textSize="@dimen/genre"
android:textStyle="italic" />
<!-- Rating -->
<RatingBar
android:id="@+id/recipe_recycler_rating_bar"
style="@style/Widget.AppCompat.RatingBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@id/recipe_recycler_publisher"
android:layout_marginTop="5dp"
android:isIndicator="true"
android:max="5" />
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
这是我的AppTheme:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
为什么会这样? CardView 中是否有错误?有关如何解决此问题的任何建议?
【问题讨论】:
-
您的
LinearLayout中的selectableItemBackground是什么颜色? -
这是用于波纹效果的背景绘制。 CardView在我申请
selectableItemBackground之前就变黑了,所以不是问题的原因。 -
您在测试您的应用的 Android 版本是什么?
-
如果是4.1.2,那个版本的
CardView变黑有问题。 -
由于这很可能是上下文问题,您应该显示一些代码,特别是您的适配器。 CardView 主题(深色或浅色)取决于您用于扩展适配器内部视图的上下文。这可以是一个 Activity 上下文(例如 getActivity())——在这种情况下,请确保您的 Activity 有一个浅色主题。或者,例如,可以是 View 上下文(例如 View.getContext()),在这种情况下 android:theme 很重要等
标签: android android-recyclerview android-cardview