Android:从首选项中选择主题颜色 [英] Android: Selecting theme color from preferences

查看:177
本文介绍了Android:从首选项中选择主题颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过偏好设置我的应用程序的导航栏的颜色按压项目,但我似乎找不到一种方法。我已经使用hex颜色代码作为值在我的首选项中创建了一个列表,我不知道如何使用我的java类中的值。帮助!

解决方案

尝试这种方法:
首先在colors.xml中定义新颜色,其值如下: / p>

 < color name =my_blue>#4FC3F7< / color> 
< color name =my_yellow>#FDD835< / color>

然后,您定义一个这样的新主题(my_theme.xml):

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

< style name =MyRandomThemeparent =Theme.AppCompat.NoActionBar>
<! - 在这里自定义您的主题。 - >
< item name =colorPrimary> @ color / my_blue< / item>
< item name =colorPrimaryDark> @ color / my_blue< / item>
< item name =android:navigationBarColor> @ color / my_blue< / item>
< / style>

< / resources>

然后,看看我如何动态更改颜色:



我的布局:

 < Button 
style =?android:attr / buttonStyleSmall
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =Blue
android:id =@ + id / btnBlue
android:layout_below =@ + id / imageView
android:layout_alignLeft =@ + id / imageView
android:layout_alignStart =@ + id / imageView
android: #162c69/>

< Button
style =?android:attr / buttonStyleSmall
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =Yellow
android:id =@ + id / btnYellow
android:layout_below =@ + id / imageView
android:layout_alignRight =@ / imageView
android:layout_alignEnd =@ + id / imageView
android:background =#9b8a09/>

< Button
style =?android:attr / buttonStyleSmall
android:layout_width =wrap_content
android:layout_height =wrap_content
android:text =Mix
android:id =@ + id / btnMix
android:layout_below =@ + id / imageView
android:layout_centerHorizo​​ntal =true
android:background =#920f08/>

我的活动:

  @Override 
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);


this.setTheme(R.style.MyRandomTheme);


setContentView(R.layout.activity_main);

//将主题更改为黄色
按钮yellowButton =(Button)findViewById(R.id.btnYellow);
yellowButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(Build.VERSION.SDK_INT> = 21){
getWindow()。getColor(R.color.my_yellow));
getWindow()。setStatusBarColor(getResources $ b}
}
});

//将主题更改为蓝色
按钮blueButton =(Button)findViewById(R.id.btnBlue);
blueButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(Build.VERSION.SDK_INT> = 21){
getWindow()。setNavigationBarColor(getResources()。getColor(R.color.my_blue));
getWindow()。setStatusBarColor(getResources()。getColor(R.color.my_blue));
}
}
});

//将主题更改为蓝色和黄色混合
按钮mixButton =(Button)findViewById(R.id.btnMix);
mixButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(Build.VERSION.SDK_INT> = 21){
getWindow()。setNavigationBarColor(getResources()。getColor(R.color.my_blue));
getWindow()。setStatusBarColor(getResources()。getColor(R.color.my_yellow));
}
}
});

}



结果: p>



此外,从偏好和写入偏好设置中读取,请尝试:

  //写入首选项
String s =这是一个测试。
SharedPreferences prefs = this.getSharedPreferences(MyPrefs,this.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(A,s);
editor.apply();
//从首选项中获取
SharedPreferences prefs2 = this.getSharedPreferences(MyPrefs,this.MODE_PRIVATE);
if(prefs2!= null){
String text2 = prefs.getString(A,);
Log.d(LOG_TAG,这是字符串:+ text2);
}

您可以传递您的参数,如R.color.my_yellow,并拥有在您的喜好中,直到您的用户更改它。我希望它有帮助。如有任何疑问,请告知我。


I wish to set the color pressed item of navigation drawer of my app through preferences but I can't seem to find a way to do so. I have created a list in my preferences with hex color codes as values and I have no idea how to use the values in my java class. HELP!

解决方案

Try this approach: First define your new colors in colors.xml in values like this:

<color name="my_blue">#4FC3F7</color>
<color name="my_yellow">#FDD835</color>

Then, you define a new theme like this (my_theme.xml):

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

    <style name="MyRandomTheme" parent="Theme.AppCompat.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/my_blue</item>
        <item name="colorPrimaryDark">@color/my_blue</item>
        <item name="android:navigationBarColor">@color/my_blue</item>
    </style>

</resources>

Then, look at how I change the color dynamically:

My layout:

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Blue"
    android:id="@+id/btnBlue"
    android:layout_below="@+id/imageView"
    android:layout_alignLeft="@+id/imageView"
    android:layout_alignStart="@+id/imageView"
    android:background="#162c69" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Yellow"
    android:id="@+id/btnYellow"
    android:layout_below="@+id/imageView"
    android:layout_alignRight="@+id/imageView"
    android:layout_alignEnd="@+id/imageView"
    android:background="#9b8a09" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Mix"
    android:id="@+id/btnMix"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:background="#920f08" />

My activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    this.setTheme(R.style.MyRandomTheme);


    setContentView(R.layout.activity_main);

    //Change theme to yellow
    Button yellowButton = (Button) findViewById(R.id.btnYellow);
    yellowButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Build.VERSION.SDK_INT >= 21) {
                getWindow().setNavigationBarColor(getResources().getColor(R.color.my_yellow));
                getWindow().setStatusBarColor(getResources().getColor(R.color.my_yellow));
            }
        }
    });

    //Change theme to blue
    Button blueButton = (Button) findViewById(R.id.btnBlue);
    blueButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Build.VERSION.SDK_INT >= 21) {
                getWindow().setNavigationBarColor(getResources().getColor(R.color.my_blue));
                getWindow().setStatusBarColor(getResources().getColor(R.color.my_blue));
            }
        }
    });

    //Change theme to mixture of blue and yellow
    Button mixButton = (Button) findViewById(R.id.btnMix);
    mixButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (Build.VERSION.SDK_INT >= 21) {
                getWindow().setNavigationBarColor(getResources().getColor(R.color.my_blue));
                getWindow().setStatusBarColor(getResources().getColor(R.color.my_yellow));
            }
        }
    });

}

The result:

Also for reading from preferences and writing to preferences try this:

        //Write to preferences
        String s = "this is a test.";
        SharedPreferences prefs = this.getSharedPreferences("MyPrefs",this.MODE_PRIVATE);
        SharedPreferences.Editor editor = prefs.edit();
        editor.putString("A", s);
        editor.apply();
        //Fetch from preferences
        SharedPreferences prefs2 = this.getSharedPreferences("MyPrefs", this.MODE_PRIVATE);
        if(prefs2 != null) {
            String text2 = prefs.getString("A","");
            Log.d(LOG_TAG, "This is the string: "+text2);
        }

You can pass your parameters like "R.color.my_yellow" and have it in your preferences till your user change it. I hope it helps. Let me know if you have any question.

这篇关于Android:从首选项中选择主题颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆