CardView突然变黑

时间:2016-07-04 13:23:58

标签: android android-recyclerview android-cardview

我对CardView中的RecyclerView有疑问。 我的CardView突然变黑了,RatingBar变成了蓝色。我使用InfiniteRecyclerView但将其更改为简单RecyclerView无效。我可以将CardView的背景颜色更改为白色,但RatingBar仍然是蓝色。这是正在发生的事情的一个例子:

Black CardView

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

Normal 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>

这是我的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中有错误吗?有关如何解决此问题的任何建议吗?

4 个答案:

答案 0 :(得分:9)

我遇到了同样的问题,这就是我如何解决它。确保适配器按如下方式对布局进行膨胀。为了让您更好地理解,请拨打此适配器 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);

    }

然后,对于您将使用适配器的活动,请确保您使用正确的上下文。现在说我们将在名为 MyActivity 的活动中使用 MyAdapter 。使用错误上下文的布局充气程序的错误方法如下

  1. 第一个错误方法

    MyAdapter adapter = new MyAdapter(
                                 getApplicationContext(), R.layout.recycler_row_layout_person, arrayList
                         );
    
  2. 第二种错误方式

    MyAdapter adapter = new MyAdapter(
                                 getContext(), R.layout.recycler_row_layout_person, arrayList
                         );
    
  3. 这是 RIGHT 的方式。这就是我解决它的方式。

    MyAdapter adapter = new MyAdapter(
                                     MyActivity.this, R.layout.recycler_row_layout_person, arrayList
                             );
    

    我希望能帮助

答案 1 :(得分:5)

我遇到了同样的问题并尝试了这个,它对我有用

card_view:cardBackgroundColor="#fff"

您的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">

答案 2 :(得分:1)

我遇到了同样的问题。 在我的情况下,我在错误的属性中定义背景,我使用默认值:

<android.support.v7.widget.CardView
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/card_view"
    android:layout_width="300dp"
    android:layout_height="200dp"
    android:layout_gravity="center"
    android:layout_marginTop="8dp"
    android:background="#ffffff">

你必须使用cardbackground属性:

 <android.support.v7.widget.CardView
    ...      
    card_view:cardBackgroundColor="#ffffff">

这对我有用。 Cardview具有自己的属性,您必须使用它们而不是默认的View属性。 提升属性也是如此。

答案 3 :(得分:1)

我遇到了同样的问题,并通过这种方式解决了

<!-- 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>
相关问题