【问题标题】:Changing theme of Android Application via CheckBox onClick通过 CheckBox onClick 更改 Android 应用程序的主题
【发布时间】:2015-05-14 07:10:14
【问题描述】:

我是一名新的 android 开发人员,我正在开发一个项目,我希望在其中一个应用程序的设置中将自定义颜色主题作为选项。我有四个复选框,分别称为 checkBox1、checkBox2、checkBox3、checkBox4。我的styles.xml 文件中还有四种自定义样式,分别称为theme1、theme2、theme3、theme4。这是我关于处理这些复选框的 onClick 的 .java 文件编码:

checkBoxListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(blueCheckBox.isChecked()){
                //Change theme to blue
            }
            if(redCheckBox.isChecked()){
                //Change theme to red
            }
            if(greenCheckBox.isChecked()){
                //Change theme to green
            }
            if(bwCheckBox.isChecked()){
                 //Change theme to Black & White
            }
        }
    };
    blueCheckBox.setOnClickListener(checkBoxListener);
    redCheckBox.setOnClickListener(checkBoxListener);
    greenCheckBox.setOnClickListener(checkBoxListener);
    bwCheckBox.setOnClickListener(checkBoxListener);
}

如何将整个应用程序的样式设置为我在这些 if 语句的主体中拥有的相应自定义声明样式?感谢您的宝贵时间,感谢您提供的任何帮助。

【问题讨论】:

    标签: java android checkbox styles


    【解决方案1】:

    为此你必须在/style/下创建一些主题

    例如:

    <resources>
        <style name="LightTheme" parent="@android:style/Theme.Light">
        </style>
    
        <style name="BlackTheme" parent="@android:style/Theme.Black">
        </style>    
    </resources>
    

    在java端你可以像...一样使用它

    if (lightThemeCheckBox.isChecked()) {
        getApplication().setTheme(R.style.LightTheme);
    } else {
        getApplication().setTheme(R.style.BlackTheme);
    }
    

    希望对你有帮助。

    更多信息请访问此链接: http://www.anddev.org/applying_a_theme_to_your_application-t817.html

    【讨论】:

    • 非常感谢你给我的代码中的 java 部分正是我想要的。
    • 所以这就是我在玩弄代码之后所拥有的,即使使用 getApplication().setTheme(R.style.myTheme) 它仍然不会改变我的应用程序的背景,并且我认为这与我的主题有关。我只是将其作为我的主题代码,例如: ​​#66cccc#17577e 这是自定义的主体风格我不认为它做我想要的。我希望我的所有按钮和文本视图的文本以及应用程序的背景都改变颜色,但没有按钮的背景
    • 正是我所需要的,再次非常感谢,非常感谢您的帮助
    【解决方案2】:

    您好,您可以通过如下设置复选框的背景来为复选框提供按钮:

    下面的代码是复选框的背景 xml 文件:名称为 checkbox_selection.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
    
    <item android:drawable="@drawable/check_active_320" android:state_checked="true" android:state_focused="true"/>
    <item android:drawable="@drawable/check_inactive_320" android:state_checked="false" android:state_focused="true"/>
    <item android:drawable="@drawable/check_inactive_320" android:state_checked="false"/>
    <item android:drawable="@drawable/check_active_320" android:state_checked="true"/>
    

    这里你发现有可绘制资源,例如当用户选中 CheckBox 时有 check_active_320 和 check_inactive_320 用于未选中的 Checkbox。

    将上述xml设置为CheckBox的代码如下:

     <CheckBox
            android:id="@+id/radioread"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textread"
            android:layout_alignBottom="@+id/textread"
            android:layout_alignParentRight="true"
            android:layout_marginRight="15dp"
            android:button="@drawable/checkbox_selection"
            android:focusable="false" />
    

    在这里你可以找到 android:button 作为我们样式的值。

    【讨论】:

      猜你喜欢
      • 2013-08-20
      • 1970-01-01
      • 2012-03-30
      • 2016-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-14
      • 1970-01-01
      相关资源
      最近更新 更多