如何改变所有活动的背景?
How to change background across all activities?
这是我改背景的代码
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.theme1:
mainLayout.setBackgroundResource(R.drawable.blackgreygradientbackground);
if (selectedBackgroundId == R.id.theme1){
Toast.makeText(getApplicationContext(), "Background already set", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Background set", Toast.LENGTH_SHORT).show();
}
selectedBackgroundId = R.id.theme1;
return true;
case R.id.theme2:
mainLayout.setBackgroundResource(R.drawable.redpinkgradientbackground);
if (selectedBackgroundId == R.id.theme2){
Toast.makeText(getApplicationContext(), "Background already set", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Background set", Toast.LENGTH_SHORT).show();
}
selectedBackgroundId = R.id.theme2;
return true;
}
}
在我的 mainactivity 中,用户可以通过单击菜单更改工具栏中的背景,它确实会更改背景,但问题是我如何在所有活动中更改它并在用户关闭应用程序并重新启动它?
所以我的问题是:
现在它只改变主要背景activity,但我想实现的是改变我所有的活动。
如何在重启时保存选择的背景?
我用自己的图片作为背景,只能找到有颜色的教程。
提前致谢,
文斯
编辑
int theme1 = R.drawable.blackgreygradientbackground;
int theme2 = R.drawable.redpinkgradientbackground;
int mSelectedBackground;
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.theme1:
mainLayout.setBackgroundResource(theme1);
mSelectedBackground = theme1;
mEditor = mSharedPreferences.edit();
mEditor.putInt("mSelectedBackground", mSelectedBackground);
mEditor.apply();
if (selectedBackgroundId == R.id.theme1){
Toast.makeText(getApplicationContext(), "Background already set", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Background set", Toast.LENGTH_SHORT).show();
}
selectedBackgroundId = R.id.theme1;
return true;
case R.id.theme2:
mainLayout.setBackgroundResource(theme2);
mSelectedBackground = theme2;
mEditor = mSharedPreferences.edit();
mEditor.putInt("mSelectedBackground", mSelectedBackground);
mEditor.apply();
if (selectedBackgroundId == R.id.theme2){
Toast.makeText(getApplicationContext(), "Background already set", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Background set", Toast.LENGTH_SHORT).show();
}
selectedBackgroundId = R.id.theme2;
return true;
}
}
所以我不确定我做的是否正确,我遵循了文档,但我现在如何将我的 activity 的主布局设置为用户在所有活动中选择的背景并保存它?
谢谢,
您可以将选定的背景保存在持久存储中(如 SharedPreferences)
并且在每个 activity 的 onCreate 上,您可以通过从保存的 SharedPreferences
中获取来设置它
示例可在此处找到:
https://developer.android.com/training/data-storage/shared-preferences.html
这是我改背景的代码
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.theme1:
mainLayout.setBackgroundResource(R.drawable.blackgreygradientbackground);
if (selectedBackgroundId == R.id.theme1){
Toast.makeText(getApplicationContext(), "Background already set", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Background set", Toast.LENGTH_SHORT).show();
}
selectedBackgroundId = R.id.theme1;
return true;
case R.id.theme2:
mainLayout.setBackgroundResource(R.drawable.redpinkgradientbackground);
if (selectedBackgroundId == R.id.theme2){
Toast.makeText(getApplicationContext(), "Background already set", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Background set", Toast.LENGTH_SHORT).show();
}
selectedBackgroundId = R.id.theme2;
return true;
}
}
在我的 mainactivity 中,用户可以通过单击菜单更改工具栏中的背景,它确实会更改背景,但问题是我如何在所有活动中更改它并在用户关闭应用程序并重新启动它?
所以我的问题是:
现在它只改变主要背景activity,但我想实现的是改变我所有的活动。
如何在重启时保存选择的背景?
我用自己的图片作为背景,只能找到有颜色的教程。
提前致谢,
文斯
编辑
int theme1 = R.drawable.blackgreygradientbackground;
int theme2 = R.drawable.redpinkgradientbackground;
int mSelectedBackground;
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.theme1:
mainLayout.setBackgroundResource(theme1);
mSelectedBackground = theme1;
mEditor = mSharedPreferences.edit();
mEditor.putInt("mSelectedBackground", mSelectedBackground);
mEditor.apply();
if (selectedBackgroundId == R.id.theme1){
Toast.makeText(getApplicationContext(), "Background already set", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Background set", Toast.LENGTH_SHORT).show();
}
selectedBackgroundId = R.id.theme1;
return true;
case R.id.theme2:
mainLayout.setBackgroundResource(theme2);
mSelectedBackground = theme2;
mEditor = mSharedPreferences.edit();
mEditor.putInt("mSelectedBackground", mSelectedBackground);
mEditor.apply();
if (selectedBackgroundId == R.id.theme2){
Toast.makeText(getApplicationContext(), "Background already set", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "Background set", Toast.LENGTH_SHORT).show();
}
selectedBackgroundId = R.id.theme2;
return true;
}
}
所以我不确定我做的是否正确,我遵循了文档,但我现在如何将我的 activity 的主布局设置为用户在所有活动中选择的背景并保存它?
谢谢,
您可以将选定的背景保存在持久存储中(如 SharedPreferences)
并且在每个 activity 的 onCreate 上,您可以通过从保存的 SharedPreferences
中获取来设置它示例可在此处找到: https://developer.android.com/training/data-storage/shared-preferences.html