猿问

切换主题,

我想在我的应用程序中添加多个主题,以允许用户在运行时更改应用程序主题。我在网上找到了代码,已成功将其添加到我的应用程序中,但我只能更改一项活动的主题,这并不是我的真正意图。当用户在主题设置活动中更改主题时,我需要将该更改应用到所有活动中。


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;

    }


叮当猫咪
浏览 120回答 2
2回答

红糖糍粑

使用属性值设置颜色例如,这是 textview 的颜色<attr&nbsp;name="textviewcolor"&nbsp;format="color"></attr>在 style.xml 中为不同的主题选择创建不同的样式这是深色主题样式的文本颜色<style&nbsp;name="AppTheme.Dark"&nbsp;parent="Theme.AppCompat.Light.NoActionBar"><item&nbsp;name="textviewcolor">#fff</item></style>这是默认主题样式的文本颜色<style&nbsp;name="AppTheme.Defult"&nbsp;parent="Theme.AppCompat.Light.NoActionBar"><item&nbsp;name="textviewcolor">#000</item></style>使用此属性值(textviewcolor)到textview来设置颜色,如下所示&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<TextView &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:layout_width="wrap_content" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:layout_height="wrap_content" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:text="text" &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;android:textColor="?attr/textviewcolor"/>您只需在按钮单击中更改共享首选项中的主题名称并刷新活动,其余的事情将在下面提到您将如何做到这一点..首先像这样创建主题util类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){&nbsp; &nbsp; int themeId=0;&nbsp; &nbsp; switch (theme){&nbsp; &nbsp; &nbsp; &nbsp; case THEME_DARK:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; themeId = R.style.AppTheme_Dark;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; case THEME_DEFAULT :&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; themeId = R.style.AppTheme;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; default:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; }&nbsp; &nbsp; return themeId;}}建议:使用共享首选项来指定主题的名称然后创建一个抽象类,通过扩展该类将主题设置为所有活动public class ChangethemeActivity extends AppCompatActivity{@Overrideprotected void onCreate(@Nullable Bundle savedInstanceState){&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; //get your theme name using sharedpreference and check what you have saved in theme name value&nbsp; &nbsp; if(dark){&nbsp; &nbsp; setTheme(ThemeUtil.getThemeId(1));&nbsp; &nbsp; }&nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; setTheme(ThemeUtil.getThemeId(2));&nbsp; &nbsp; }&nbsp; }}}最后:在要更改主题的活动中使用 ChangethemeActivity而不是 AppCompatActivity这是我实现应用程序主题更改功能的方式,请告诉我这是否解决了您的问题

神不在的星期二

创建一个基本活动并从中扩展所有其他活动,然后在基本活动的 onCreate 方法中设置主题,例如:public abstract class BaseActivity extends AppCompatActivity{&nbsp; &nbsp; private int theme;&nbsp; &nbsp; @Override&nbsp; &nbsp; protected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; &nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; &nbsp; &nbsp; setTheme(theme);&nbsp; &nbsp; }}private void setTheme(int theme){&nbsp; &nbsp; this.theme = theme;}然后,当您想要更改主题时,只需在 theme 中设置另一个主题资源 id 并重新启动您的 Activity:setTheme(R.style.AppThemeChristmas);Intent intent = getIntent();finish();startActivity(intent);
随时随地看视频慕课网APP

相关分类

Java
我要回答