【问题标题】:Shared Preferences Drawable color Android共享首选项可绘制颜色 Android
【发布时间】:2017-05-10 14:13:45
【问题描述】:

您好,我正在使用 Shared Preferences 来存储 APP 的颜色。 到目前为止,我可以保存按钮和状态栏的颜色,因为它们不使用可绘制颜色。

这是我的功能:

private void storeColor(int color){
    SharedPreferences mSharedpreferences = getSharedPreferences("ToolbarColor", MODE_PRIVATE);
    SharedPreferences.Editor mEditor = mSharedpreferences.edit();
    mEditor.putInt("color", color);
    mEditor.apply();
}

private int getColor(){
    SharedPreferences mSharedPreferences = getSharedPreferences("ToolbarColor", MODE_PRIVATE);
    int selectedColor = mSharedPreferences.getInt("color", getResources().getColor(R.color.colorPrimary));

    return selectedColor;
}

我是这样使用它们的:

if (intValue == 1){
            getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.green_apple));
            btn_historial.setBackgroundColor(getResources().getColor(R.color.green_apple));
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setStatusBarColor(getResources().getColor(R.color.green_apple));
            }
            storeColor(getResources().getColor(R.color.green_apple));
        }


if(getColor() != getResources().getColor(R.color.colorPrimary)){
            getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.color.colorPrimary));
            btn_historial.setBackgroundColor(getColor());
            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                getWindow().setStatusBarColor(getColor());
            }
        }

以前我使用工具栏,但现在我想使用操作栏。 我有点迷路了如何在这里添加可绘制的颜色?

【问题讨论】:

  • 你想从共享偏好中添加颜色是你的问题吗??
  • 嗯,那么,您想从您的共享首选项中为您的操作栏添加背景颜色吗?而且您不确定如何在 sharepreference 中保存可绘制对象?
  • @ArpitPatel 是的
  • 将您的 hexa 代码保存为首选字符串,并使用类似 Color.parseColor(preference value) 进行设置
  • @ArpitPatel 怎么做?当我打印行时,它说 new bgColorKey = android.graphics.drawable.ColorDrawable@91cdd14 它没有读取解析的十六进制值

标签: android android-actionbar sharedpreferences drawable android-drawable


【解决方案1】:

使用ColorDrawable

int color = getColor();
getActionBar().setBackgroundDrawable(new ColorDrawable(color));

【讨论】:

  • 也许更好Color.parseColor(color);
  • @marlonpya 我认为它说的是整数颜色。 Color.parseColor(color); 用于字符串。
猜你喜欢
  • 1970-01-01
  • 2012-03-20
  • 1970-01-01
  • 1970-01-01
  • 2020-06-30
  • 1970-01-01
  • 2015-09-10
  • 2012-09-20
  • 2012-09-03
相关资源
最近更新 更多