【问题标题】:How to set random background for cardview?如何为cardview设置随机背景?
【发布时间】:2017-06-29 17:38:08
【问题描述】:

我目前有一个 Android recyclerview 和一个列表项。在列表项中是我的观点的卡片视图。我想为每张卡片列出随机背景:
我的卡片现在有一个坚实的背景,我到处搜索并使用了任何代码,但找不到类似示例的示例。

我的列表项:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/card_view_lead"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    app:cardCornerRadius="10dp"
    app:cardPreventCornerOverlap="false">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/style_lead"
        android:padding="7dp">

        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:background="@android:color/transparent"
            android:contentDescription="@string/option"
            android:src="@drawable/ic_option"
            android:tint="@android:color/white" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/large_margin"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <com.makeramen.roundedimageview.RoundedImageView
                    android:id="@+id/lead_img"
                    android:layout_width="40dp"
                    android:layout_height="40dp"
                    android:contentDescription="@string/test_pic"
                    app:riv_border_color="@color/colorPrimary"
                    app:riv_border_width="0.1dp"
                    app:riv_corner_radius="100dp"
                    tools:src="@drawable/pic_1" />

                <TextView
                    android:id="@+id/lead_name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:layout_marginLeft="@dimen/standard_margin"
                    android:layout_toRightOf="@id/lead_img"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/large_font_size"
                    tools:text="@string/test_name" />

            </RelativeLayout>

            <TextView
                android:id="@+id/lead_city"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/standard_margin"
                android:layout_marginTop="16dp"
                android:textColor="@android:color/white"
                android:textSize="@dimen/large_font_size"
                tools:text="@string/test_city" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/standard_margin">

                <TextView
                    android:id="@+id/lead_price"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="16dp"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/large_font_size"
                    tools:text="30$" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/large_margin"
                    android:layout_marginTop="16dp"
                    android:layout_toRightOf="@id/lead_price"
                    android:text="@string/per_hour"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/large_font_size" />

            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="20dp"
                android:layout_marginTop="16dp">

                <TextView
                    android:id="@+id/with"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/standard_margin"
                    android:text="@string/with"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/large_font_size" />

                <TextView
                    android:id="@+id/lead_vehicle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="6dp"
                    android:layout_toRightOf="@id/with"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/large_font_size"
                    tools:text="@string/car" />

                <ImageView
                    android:id="@+id/lead_vehicle_img"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="8dp"
                    android:layout_toRightOf="@id/lead_vehicle"
                    android:contentDescription="@string/car_img"
                    tools:src="@drawable/ic_car" />
            </RelativeLayout>
        </LinearLayout>
    </RelativeLayout>

</android.support.v7.widget.CardView>

【问题讨论】:

  • 你想用随机颜色作为你的卡片视图背景吗??
  • @Moulesh 是的。我在描述中说。
  • @A.Heydari 我的旧答案呢???
  • 检查我的答案,它会起作用...

标签: android random android-recyclerview android-cardview


【解决方案1】:

您可以在每次将新项目放入适配器列表时生成随机颜色 See this


由于您只提供了 xml 部分,我假设您有一个模型来存储要显示的信息。您可以添加另一个属性“颜色”,您可以在其中存储每个项目的随机颜色,并在您的 onBindViewHolder 方法中设置

【讨论】:

  • @A.Heydari object 'card' 应该为 null,它会导致 NullPointerException?调试你的代码可以帮助你分析它!
  • 看看this
【解决方案2】:
CardView cardView = (CardView)findViewById(R.id.card_view_lead);
cardView.setCardBackgroundColor(getRandomColorCode());

public int getRandomColorCode(){

  Random random = new Random();

  return Color.argb(255, random.nextInt(256), random.nextInt(256),     random.nextInt(256));

}

【讨论】:

  • 我们可以使用随机渐变颜色吗?
【解决方案3】:
CardView card = (CardView)findViewById(R.id.card_view_lead);  

在您的 onbindview 支架中

Random rnd = new Random();
        currentColor = Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    holder.card.setCardBackgroundColor(currentColor);

【讨论】:

  • nullpointerexaption for card.setBackgroundColor(currentColor);
  • 这意味着您没有分配 cardview 检查更新答案的参考。希望您知道如何参考来自适配器的视图
【解决方案4】:

像这样在 res/values/color 中创建数组

<array name="note_neutral_colors">
    <item>#9E9E9E</item>
    <item>#455A64</item>
    <item>#607D8B</item>
</array>

<array name="note_accent_colors">
    <item>#039BE5</item>
    <item>#3D51B3</item>
    <item>#689f38</item>
    <item>#FD7044</item>
</array>

在你的适配器类中

holder.cardView.setCardBackgroundColor(noticeModel.getColor());

模型类

//Variable
int color;
//Cunstructor
this.color=color;  
///and generate Getter Setter

在你的 Main Class.java 中执行此操作

private static int getRandomColor(Context context) {
    int[] colors;
    if (Math.random() >= 0.6) {
        colors = context.getResources().getIntArray(R.array.note_accent_colors);
    } else {
        colors = context.getResources().getIntArray(R.array.note_neutral_colors);
    }
    return colors[((int) (Math.random() * colors.length))];
}

用这样的字符串请求调用这个方法

yourModel = new Your_Model(data1,data2,data3,getRandomColor(YourActivity.this));

【讨论】:

  • 我必须在哪里使用:yourModel = new Your_Model(data1,data2,data3,getRandomColor(YourActivity.this));
  • @A.Heydari 它用于您将数据输入模型的活动中。
  • 当您使用 RecyclerView 时,此答案非常有效。谢谢!
【解决方案5】:

你需要编写一些逻辑来产生随机颜色,然后像这样使用它

CardView card = (CardView)findViewById(R.id.card);
card.setCardBackgroundColor(color);

要生成随机颜色,Stack Overflow 上已经提供了解决方案,请查看this 链接。

【讨论】:

  • 我试过这个,但我确实得到了一个错误'空指针异常';如果您对 java 代码中的随机颜色有任何想法,请发给我
【解决方案6】:

通常我们可以这样做, 颜色.xml

    <?xml version="1.0" encoding="utf-8"?>
<resources>

    <item name="blue" type="color">#FF33B5E5</item>
    <item name="purple" type="color">#FFAA66CC</item>
    <item name="green" type="color">#FF99CC00</item>
    <item name="orange" type="color">#FFFFBB33</item>
    <item name="red" type="color">#FFFF4444</item>
    <item name="darkblue" type="color">#FF0099CC</item>
    <item name="darkpurple" type="color">#FF9933CC</item>
    <item name="darkgreen" type="color">#FF669900</item>
    <item name="darkorange" type="color">#FFFF8800</item>
    <item name="darkred" type="color">#FFCC0000</item>

    <integer-array name="androidcolors">
        <item>@color/blue</item>
        <item>@color/purple</item>
        <item>@color/green</item>
        <item>@color/orange</item>
        <item>@color/red</item>
        <item>@color/darkblue</item>
        <item>@color/darkpurple</item>
        <item>@color/darkgreen</item>
        <item>@color/darkorange</item>
        <item>@color/darkred</item>
    </integer-array>

</resources>

并在您的 onCreateView() 中执行此操作,

CardView card_view_lead;
         card_view_lead= (CardView) findViewById(R.id.card_view_lead);

        int[] androidColors = getResources().getIntArray(R.array.androidcolors);
        int randomAndroidColor = androidColors[new Random().nextInt(androidColors.length)];
        card_view_lead.setBackgroundColor(randomAndroidColor);

如果您不想使用颜色数组,那么您可以在 onCreateView() 或 onCreate() 中像这样使用,

mCardViewTop.setCardBackgroundColor(getRandomColor());
public static int getRandomColor() {
        Random rnd = new Random();
        return Color.argb(255, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-07-21
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-23
    相关资源
    最近更新 更多