【问题标题】:Android: Selecting theme color from preferencesAndroid:从首选项中选择主题颜色
【发布时间】:2014-12-18 16:24:46
【问题描述】:

我希望通过首选项设置我的应用导航抽屉的颜色按下项,但我似乎找不到这样做的方法。我在我的首选项中创建了一个以十六进制颜色代码作为值的列表,但我不知道如何在我的 java 类中使用这些值。帮助!

【问题讨论】:

    标签: android eclipse


    【解决方案1】:

    试试这个方法: 首先在 colors.xml 中定义新颜色,如下所示:

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

    然后,您像这样定义一个新主题 (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>
    

    然后,看看我是如何动态改变颜色的:

    我的布局:

    <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" />
    

    我的活动:

    @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));
                }
            }
        });
    

    }

    结果:

    也可以从首选项读取和写入首选项试试这个:

            //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);
            }
    

    您可以传递诸如“R.color.my_yellow”之类的参数并将其放在您的首选项中,直到您的用户更改它。我希望它有所帮助。如果您有任何问题,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-01
      • 2019-04-21
      相关资源
      最近更新 更多