切换主题
Switching themes
我想在我的应用程序中添加多个主题,以允许用户在运行时更改应用程序主题。我在网上找到了已成功添加到我的应用程序中的代码,但我只能更改一个 activity 的主题,这并不是我的本意。当用户在主题设置 activity 中更改主题时,我需要在所有活动中应用该更改。
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;
}
}
创建一个基础 activity 并从中扩展所有其他基础,然后在基础 activity 的 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;
}
然后,当你想改变主题时,只需在主题中设置另一个主题资源id并重新启动你的activity:
setTheme(R.style.AppThemeChristmas);
Intent intent = getIntent();
finish();
startActivity(intent);
使用属性值设置颜色
例如,这里是 textview 的颜色
<attr name="textviewcolor" format="color"></attr>
在 style.xml
中为不同的主题选择创建不同的样式
这里是深色主题风格的文本颜色
<style name="AppTheme.Dark" parent="Theme.AppCompat.Light.NoActionBar"><item name="textviewcolor">#fff</item></style>
这是默认主题样式的文本颜色
<style name="AppTheme.Defult" parent="Theme.AppCompat.Light.NoActionBar"><item name="textviewcolor">#000</item></style>
使用这个属性值(textviewcolor)给textview设置颜色这样
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"
android:textColor="?attr/textviewcolor"/>
您只需在单击按钮时更改 sharedpreference 中的主题名称并刷新 activity 其余的事情在下面提到了您将如何做..
首先像这样创建主题实用程序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;
}}
建议: 使用 sharedpreference 给你的主题命名
然后创建一个摘要 class 以将主题设置为所有 activity 使用扩展 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));
} }}}
最后: 在 activity 中使用 ChangethemeActivity 而不是在 activity 中你想要的 AppCompatActivity更改主题
这是我实现应用程序主题更改功能的方式 如果这解决了您的问题,请告诉我
我想在我的应用程序中添加多个主题,以允许用户在运行时更改应用程序主题。我在网上找到了已成功添加到我的应用程序中的代码,但我只能更改一个 activity 的主题,这并不是我的本意。当用户在主题设置 activity 中更改主题时,我需要在所有活动中应用该更改。
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;
}
}
创建一个基础 activity 并从中扩展所有其他基础,然后在基础 activity 的 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;
}
然后,当你想改变主题时,只需在主题中设置另一个主题资源id并重新启动你的activity:
setTheme(R.style.AppThemeChristmas);
Intent intent = getIntent();
finish();
startActivity(intent);
使用属性值设置颜色
例如,这里是 textview 的颜色
<attr name="textviewcolor" format="color"></attr>
在 style.xml
中为不同的主题选择创建不同的样式这里是深色主题风格的文本颜色
<style name="AppTheme.Dark" parent="Theme.AppCompat.Light.NoActionBar"><item name="textviewcolor">#fff</item></style>
这是默认主题样式的文本颜色
<style name="AppTheme.Defult" parent="Theme.AppCompat.Light.NoActionBar"><item name="textviewcolor">#000</item></style>
使用这个属性值(textviewcolor)给textview设置颜色这样
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="text"
android:textColor="?attr/textviewcolor"/>
您只需在单击按钮时更改 sharedpreference 中的主题名称并刷新 activity 其余的事情在下面提到了您将如何做..
首先像这样创建主题实用程序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;
}}
建议: 使用 sharedpreference 给你的主题命名
然后创建一个摘要 class 以将主题设置为所有 activity 使用扩展 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));
} }}}
最后: 在 activity 中使用 ChangethemeActivity 而不是在 activity 中你想要的 AppCompatActivity更改主题
这是我实现应用程序主题更改功能的方式 如果这解决了您的问题,请告诉我