【问题标题】:CardView suddenly turned blackCardView突然变黑了
【发布时间】:2016-07-04 13:23:57
【问题描述】:

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

【问题讨论】:

  • 您的LinearLayout 中的selectableItemBackground 是什么颜色?
  • 这是用于波纹效果的背景绘制。 CardView在我申请selectableItemBackground之前就变黑了,所以不是问题的原因。
  • 您在测试您的应用的 Android 版本是什么?
  • 如果是4.1.2,那个版本的CardView变黑有问题。
  • 由于这很可能是上下文问题,您应该显示一些代码,特别是您的适配器。 CardView 主题(深色或浅色)取决于您用于扩展适配器内部视图的上下文。这可以是一个 Activity 上下文(例如 getActivity())——在这种情况下,请确保您的 Activity 有一个浅色主题。或者,例如,可以是 View 上下文(例如 View.getContext()),在这种情况下 android:theme 很重要等

标签: android android-recyclerview android-cardview


【解决方案1】:

我遇到了同样的问题,我是这样解决的。确保您的 Adapter 按如下方式扩展布局。为了让您更好地理解,我们将这个适配器称为 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);

    }

然后,对于您将在其中使用适配器的 Activity,请确保您使用的是正确的上下文。现在假设我们将在名为 MyActivity 的活动中使用 MyAdapter错误错误上下文中使用layout inflator的方法如下

  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
                         );
    

这是正确的方法。这就是我解决它的方法。

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

希望对你有帮助

【讨论】:

  • 看起来这段代码是用于ListView的适配器,而不是RecyclerView
  • 是的,问题在于使用了错误的上下文!谢谢!
【解决方案2】:

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

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

【讨论】:

  • 我没有发现上下文被引用的方式有任何问题,但这很有效。我想也许如果你将卡片视图包装在其他东西中,它也可能不是问题。
【解决方案3】:

我遇到了同样的问题,用这种风格解决了

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

【讨论】:

    【解决方案4】:

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

    <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 属性。 海拔属性也是如此。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-13
      相关资源
      最近更新 更多