[英]Theme not getting set on Main Activity
I am trying to change theme of an application dynamically.我正在尝试动态更改应用程序的主题。 I have two activities, one is main activity and another is setting activity.
我有两个活动,一个是主要活动,另一个是设置活动。
In settings activity I am changing theme from color picker dialog when color is selected, theme is changed.在设置活动中,我正在选择颜色时从颜色选择器对话框中更改主题,主题会更改。 But theme is only getting change for the settings activity and not on main activity.
但是主题仅针对设置活动而不是主要活动进行更改。
I tried to send color values from settings activity to main activity , but the app dose not get start up.我尝试将颜色值从设置活动发送到主要活动,但应用程序无法启动。 It's showing only blank screen.
它只显示空白屏幕。
When I did debug, I came to know that its getting stuck on first if(n==0){}
, and the loop goes on it dose not end.当我进行调试时,我开始知道它首先卡在
if(n==0){}
上,并且循环继续进行并没有结束。
Not getting what's wrong.不明白怎么回事。
Also may be main activity dose not get refreshed when I come from settings activity.当我来自设置活动时,也可能是主要活动没有刷新。 How can I refresh main activity?
如何刷新主要活动?
Settings Activity:设置活动:
public class Settings extends AppCompatActivity {
int no;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final int color = 0;
public static final int color_2 = 0;
public static final int color_3 = 0;
SharedPreferences sharedpreferences;
@Override
protected void onCreate(Bundle savedInstanceState) {
Theme.onActivityCreateSetTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
final ColorPickerDialog colorPickerDialog = new ColorPickerDialog();
colorPickerDialog.initialize(R.string.dialog_title, new int[]{Color.CYAN, Color.LTGRAY, Color.BLACK, Color.BLUE, Color.GREEN, Color.MAGENTA, Color.RED, Color.GRAY, Color.YELLOW}, Color.YELLOW, 3, 2);
colorPickerDialog.setOnColorSelectedListener(new ColorPickerSwatch.OnColorSelectedListener() {
@Override
public void onColorSelected(int color) {
if (color == Color.CYAN) {
Theme.changeToTheme(Settings.this, Theme.THEME_DEFAULT);
no = 0;
}
else if (color == Color.LTGRAY)
{
Theme.changeToTheme(Settings.this, Theme.THEME_WHITE);
no = 1;
}
else if (color == Color.BLACK) {
Theme.changeToTheme(Settings.this,Theme.THEME_BLUE);
no = 3;
}
Toast.makeText(Settings.this, "selectedColor : " + color, Toast.LENGTH_SHORT).show();
}
});
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putInt("color",no);
// editor.putInt("color_2",no);
// editor.putInt("color_3",no);
editor.commit();
LinearLayout theme = (LinearLayout)findViewById(R.id.theme);
theme.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
colorPickerDialog.show(getSupportFragmentManager(), "colorpicker");
}
});
}
}
Theme:主题:
public class Theme {
private static int sTheme;
public final static int THEME_DEFAULT = 0;
public final static int THEME_WHITE = 1;
public final static int THEME_BLUE = 2;
/** * Set the theme of the Activity, and restart it by creating a new Activity of the same type. */
public static void changeToTheme(Activity activity, int theme) {
sTheme = theme;
activity.finish();
Intent i = new Intent(activity,activity.getClass());
activity.startActivity(i);
}
/** Set the theme of the activity, according to the configuration. */
public static void onActivityCreateSetTheme(Activity activity) {
switch (sTheme) {
default:
case THEME_DEFAULT:
activity.setTheme(R.style.AppTheme);
break;
case THEME_WHITE:
activity.setTheme(R.style.AppTheme_Solarized);
break;
case THEME_BLUE:
activity.setTheme(R.style.BlueTheme);
break;
}
}
}
Main Activity:主要活动:
public class MainActivity extends AppCompatActivity {
private NavigationView navigationView;
private DrawerLayout drawerLayout;
Toolbar mToolbar;
private MainFragment mFragment;
private int no,no1,no2;
@Override
protected void onCreate(Bundle savedInstanceState) {
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
no = pref.getInt("color",0);
if(no == 0)
{
setTheme(R.style.AppTheme);
}
else if(no == 1)
{
setTheme(R.style.AppTheme_Solarized);
}
else if(no == 2)
{
setTheme(R.style.BlueTheme);
}
// Theme.onActivityCreateSetTheme(this);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thank you.谢谢你。
In your Settings
class you are writing the selected color into "MyPrefs"
preferences, but in your MainActivity
your are reading from "MyPref"
(without S) preferences.在您的
Settings
类中,您将所选颜色写入"MyPrefs"
首选项,但在您的MainActivity
中,您正在从"MyPref"
(不带 S)首选项中读取。
You have already had the public constant MyPREFERENCES
, try to use it in the MainActivity
also.您已经拥有公共常量
MyPREFERENCES
,请尝试在MainActivity
中使用它。
When i changed the theme of the second activity, i couldn't change the first activities theme.当我更改第二个活动的主题时,我无法更改第一个活动的主题。 I wrote this code in both activities
我在这两个活动中都写了这段代码
ChangeThemes.onActivityCreateSetTheme(this);
Just right before就在之前
super.onCreate(savedInstanceState);
It worked!有效!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.