我可以根据变量设置android背景颜色吗?

时间:2016-04-30 11:52:48

标签: java android background-color

这是我的代码:

private LinearLayout erBackground;
erBackground = (LinearLayout) findViewById(R.id.erBackground);

String background = "YELLOW"
erBackground.setBackgroundColor(Color.background)

我需要对背景做些什么才能让背景变为黄色?

以上是我的代码的简化版本。我想要做的是将一个项目从一个活动的微调器(“黄色”)传递到共享首选项,然后创建下一个活动的创建我要检查背景是否存储在共享首选项中...如果是将颜色更改为此。

OnClick代码:

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();

editor.putString("background", personaliseBackgroundSpinner.getSelectedItem().toString().toUpperCase());
editor.putString("font", personaliseFontSpinner.getSelectedItem().toString().toUpperCase());

editor.commit();

下一个活动的OnCreate代码:

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();


String background = pref.getString("background", null); // background = "YELLOW"

if (background != null) {
    erBackground.setBackgroundColor(Color.);
}

共享首选项部分有效,我的问题是它不允许我在颜色后使用变量并希望我使用颜色名称,但如果颜色发生变化怎么办?对于每种颜色,我是否需要一个很长的if if语句?

编辑: 第二个活动的XML布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/erBackground">

<TextView
    android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed neque lacus, tempus eget ultrices id, efficitur id metus. Suspendisse convallis quam vel orci congue lobortis. Praesent et venenatis tortor. Nam non venenatis erat, eget tempus metus."
    android:id="@+id/erUserResultsTextView"
    android:layout_below="@+id/erSubmitBtn"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="41dp" />

</LinearLayout>

3 个答案:

答案 0 :(得分:2)

使用此代码......

  erBackground.setBackgroundColor(Color.YELLOW)

颜色是类,黄色 int 不是字符串 ......

请在使用颜色前查看此内容: - http://developer.android.com/intl/es/reference/android/graphics/Color.html

使用此代码解决您的问题.....

将此变量声明为全局.......

int color[]={Color.YELLOW,Color.GREEN,Color.BLUE,Color.RED,Color.BLACK};

String spinnerText[]={"Yellow","Green","Blue","Red","Black"};//set it to the spinner values


> use the same pattern for the spinner in same order as color.
> Get the position of the spinner and set it in color array variable. like  `erBackground.setBackgroundColor(color[spinner.getSelectedItemPosition()]);`
> It is save lots of code of yours.

享受编码...........

答案 1 :(得分:0)

String background = "#fff" //whatever color you want to put here put in hex 
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("background",background );
editor.putString("font",   personaliseFontSpinner.getSelectedItem().toString().toUpperCase());
editor.commit();

在下一个Activity

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
String background = pref.getString("background", null);
if (background != null) {
erBackground.setBackgroundColor(Color.parseColor(background));
} 

答案 2 :(得分:0)

您可以创建一个HashMap来映射颜色十六进制代码和名称。例如。

HashMap<String, String> map = new HashMap<String, String>();
map.putString("Black","#000000");
map.putString("White","#FFFFFF");

传递从微调器获得的颜色名称,并获得该名称的相应值

String value=(String)map.get("Black");

并设置为背景,你可以这样做

erBackground.setBackgroundColor(Color.parseColor(value));