简体   繁体   English

切换主题

[英]Switching themes

I want to add multiple themes in my app to allow users to change the app themes at runtime.我想在我的应用程序中添加多个主题,以允许用户在运行时更改应用程序主题。 I found code online which I have successfully added to my app, but I'm only able to change the theme of one activity which is not really my intention.我在网上找到了已成功添加到我的应用程序的代码,但我只能更改一项活动的主题,这并不是我真正的意图。 When a user changes theme in the theme settings activity, I need that change to be applied in all activities.当用户在主题设置活动中更改主题时,我需要将该更改应用于所有活动。

ThemeActivity.java

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    themeUtils.onActivityCreateSetTheme(this);
    setContentView(R.layout.activity_theme);


    blackbtn = findViewById(R.id.blackbutton);
    bluebtn = findViewById(R.id.bluebutton);

    blackbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            themeUtils.changeToTheme(ThemeActivity.this, themeUtils.BLACK);
        }
    });

    bluebtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            themeUtils.changeToTheme(ThemeActivity.this, themeUtils.BLUE);
        }
    });

}

ThemeUtils.java

public class themeUtils
{

private static int cTheme;
public final static int BLACK = 0;
public final static int BLUE = 1;

public static void changeToTheme(Activity activity, int theme)
{
    cTheme = theme;
    activity.finish();
    activity.startActivity(new Intent(activity, activity.getClass()));
}

public static void onActivityCreateSetTheme(Activity activity)
{
    switch (cTheme)
    {

        default:
        case BLACK:
            activity.setTheme(R.style.BlackTheme);
            break;
        case BLUE:
            activity.setTheme(R.style.BlueTheme);
            break;
    }
}

Create a base activity and extend all others from it, then in the onCreate method of the base activity set the theme, like:创建一个基础活动并从中扩展所有其他活动,然后在基础活动的 onCreate 方法中设置主题,例如:

public abstract class BaseActivity extends AppCompatActivity{
    private int theme;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(theme);
    }
}

private void setTheme(int theme){
    this.theme = theme;
}

Then, when you want to change the theme, just set another theme resource id in theme and restart your activity:然后,当您想更改主题时,只需在主题中设置另一个主题资源 id 并重新启动您的活动:

setTheme(R.style.AppThemeChristmas);
Intent intent = getIntent();
finish();
startActivity(intent);

use attribute value to set the color使用属性值设置颜色

for exmaple here's the color for textview例如这里是 textview 的颜色

<attr name="textviewcolor" format="color"></attr>

create differet styles for different theme selection in style.xml为 style.xml 中的不同主题选择创建不同的 styles

here's the textcolor for dark theme style这是深色主题样式的文本颜色

<style name="AppTheme.Dark" parent="Theme.AppCompat.Light.NoActionBar"><item name="textviewcolor">#fff</item></style>

here's the textcolor for defult theme style这是默认主题样式的文本颜色

<style name="AppTheme.Defult" parent="Theme.AppCompat.Light.NoActionBar"><item name="textviewcolor">#000</item></style>

use this attribute value(textviewcolor) to textview to set color like this使用这个属性值(textviewcolor)到 textview 来设置这样的颜色

                      <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="text"
                        android:textColor="?attr/textviewcolor"/>

you just have to change the theme name in sharedpreference in button click and refresh the activity rest of things are mentioned below how you gonna do it..您只需要在按钮单击中更改 sharedpreference 中的主题名称并刷新活动 rest 下面提到了您将如何做的事情。

first create theme util class like this首先像这样创建主题 util class

public class ThemeUtil {

public static final int THEME_DEFAULT=1;
public static final int THEME_DARK=2;
public static final int ALERTTHEME=3;
public static final int ALERTTHEMEDARK=4;



public static int getThemeId(int theme){
    int themeId=0;
    switch (theme){

        case THEME_DARK:
            themeId = R.style.AppTheme_Dark;
            break;

        case THEME_DEFAULT :
            themeId = R.style.AppTheme;
            break;

        default:
            break;
    }
    return themeId;
}}

suggestion: use sharedpreference to give name of your theme建议:使用 sharedpreference 为您的主题命名

then create one abstract class to set theme to all the activity using extending that class然后创建一个抽象 class 以使用扩展 class 为所有活动设置主题

public class ChangethemeActivity extends AppCompatActivity{

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

    //get your theme name using sharedpreference and check what you have saved in theme name value

    if(dark){
    setTheme(ThemeUtil.getThemeId(1));
    }
    else{
      setTheme(ThemeUtil.getThemeId(2));
    }  }}}

and finally: use ChangethemeActivity in activity instead of AppCompatActivity in your activity where you want to change the theme最后:在要更改主题的活动中使用ChangethemeActivity而不是 AppCompatActivity

this is the way i've achieved theme change feature to app let me know if this solved your problem这是我为应用程序实现主题更改功能的方式让我知道这是否解决了您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM