CardView突然变黑了

fro*_*zyk 5 android android-cardview android-recyclerview

我有一个问题与CardView在RecyclerView.我CardView突然变黑了,RatingBar变成了蓝色.我正在使用InfiniteRecyclerView但改变它简单RecyclerView没有效果.我可以将CardView背景颜色改为白色,但RatingBar仍然是蓝色.这是正在发生的事情的一个例子:

黑色CardView

我使用RecyclerView相同适配器的片段中的另一个活动中的法线,它看起来很好.这是一个例子:

普通的CardView

这是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>
Run Code Online (Sandbox Code Playgroud)

这是我的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>
Run Code Online (Sandbox Code Playgroud)

为什么会这样?有没有错误CardView?对于如何解决这个问题,有任何的建议吗?

小智 11

我遇到了同样的问题,这就是我如何解决它.确保适配器按如下方式对布局进行充气.为了让您更好地理解,请调用此适配器MyAdapter

@Override
public View getView(int position, View view, ViewGroup parent) {
    if (view == null){
        LayoutInflater layoutInflater = (LayoutInflater) getContext()
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        view = layoutInflater.inflate(R.layout.recycler_row_layout_person, null, true);

    }
Run Code Online (Sandbox Code Playgroud)

然后,对于您将使用适配器的Activity,请确保使用正确的Context.现在说我们将使用MyAdapter在所谓的活动MyActivity.在错误使用的布局充气用的方法错了是上下文如下

  1. 第一个错误的方式

    MyAdapter adapter = new MyAdapter(
                                 getApplicationContext(), R.layout.recycler_row_layout_person, arrayList
                         );
    
    Run Code Online (Sandbox Code Playgroud)
  2. 第二个错误的方式

    MyAdapter adapter = new MyAdapter(
                                 getContext(), R.layout.recycler_row_layout_person, arrayList
                         );
    
    Run Code Online (Sandbox Code Playgroud)

这是正确的做法.这就是我解决它的方式.

MyAdapter adapter = new MyAdapter(
                                 MyActivity.this, R.layout.recycler_row_layout_person, arrayList
                         );
Run Code Online (Sandbox Code Playgroud)

我希望它有所帮助


ysf*_*yln 6

我有同样的问题并试过这个,它对我有用

card_view:cardBackgroundColor="#fff"
Run Code Online (Sandbox Code Playgroud)

您的XML文件

<android.support.v7.widget.CardView 
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
card_view:cardBackgroundColor="#fff">
Run Code Online (Sandbox Code Playgroud)


小智 5

我遇到了同样的问题并通过这种风格解决了

<!-- Base application theme. -->
<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>
    <item name="cardViewStyle">@style/CardView.Light</item>
</style>
Run Code Online (Sandbox Code Playgroud)