You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改CardView背景色并保存更改?已调用setCardBackgroundColor方法

Hey there! Let's work through how to make that CardView background color change stick even after you close and reopen the view. The core issue here is that your current color change only lives in memory—when the view or activity is recreated, it resets to the original value. We need to persist the color choice so it can be loaded again later.

Saving CardView Background Color Changes

The simplest way to handle this light-weight persistence in Android is using SharedPreferences, which lets you store key-value pairs easily. Here's a step-by-step breakdown:

1. Save the Color Value When You Change It

Right after you call cardView.setCardBackgroundColor(Color.BLUE), save the color's integer value to SharedPreferences:

Kotlin Example

// Get a SharedPreferences instance (use a unique name for your app's preferences)
val prefs = getSharedPreferences("CardViewSettings", Context.MODE_PRIVATE)
// Edit and save the color
prefs.edit().apply {
    putInt("saved_card_bg_color", Color.BLUE)
    apply() // Async save, won't block the main thread
}

Java Example

SharedPreferences prefs = getSharedPreferences("CardViewSettings", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("saved_card_bg_color", Color.BLUE);
editor.apply();

2. Load and Apply the Saved Color on View Creation

When your activity or fragment initializes (like in onCreate or onViewCreated), fetch the saved color and apply it to the CardView:

Kotlin Example

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.your_layout_file)

    val cardView = findViewById<CardView>(R.id.your_card_view_id)
    val prefs = getSharedPreferences("CardViewSettings", Context.MODE_PRIVATE)

    // Use your original default color as the fallback if no saved value exists
    val savedColor = prefs.getInt("saved_card_bg_color", Color.WHITE)
    cardView.setCardBackgroundColor(savedColor)
}

Java Example

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout_file);

    CardView cardView = findViewById(R.id.your_card_view_id);
    SharedPreferences prefs = getSharedPreferences("CardViewSettings", Context.MODE_PRIVATE);

    int savedColor = prefs.getInt("saved_card_bg_color", Color.WHITE);
    cardView.setCardBackgroundColor(savedColor);
}

Quick Extra Tips

  • If you're letting users pick a color from a color picker, just replace Color.BLUE with the integer value of the color they selected.
  • To reset the color to default, you can remove the saved key: prefs.edit().remove("saved_card_bg_color").apply().

This way, every time you open the view, the CardView will automatically use the last color you set!

内容的提问来源于stack exchange,提问作者Keerthi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.13 08:21:22