我创建了一个PreferenceActivity,允许用户选择他想要应用于整个应用程序的主题.
当用户选择主题时,执行以下代码:
if (...) {
getApplication().setTheme(R.style.BlackTheme);
} else {
getApplication().setTheme(R.style.LightTheme);
}
Run Code Online (Sandbox Code Playgroud)
但是,即使我已经使用调试器检查了代码是否正在执行,我也看不到用户界面的任何变化.
主题是定义的res/values/styles.xml,Eclipse没有显示任何错误.
<resources>
<style name="LightTheme" parent="@android:style/Theme.Light">
</style>
<style name="BlackTheme" parent="@android:style/Theme.Black">
</style>
</resources>
Run Code Online (Sandbox Code Playgroud)
关于可能发生的事情以及如何解决问题的任何想法?我应该拨打setTheme代码中的任何特殊点吗?如果有帮助,我的应用程序包含几个活动.
Pen*_*m10 84
我也希望看到这个方法,你可以为你的所有活动设置一次.但据我所知,你必须在显示任何观点之前设置每个活动.
供参考检查:
http://www.anddev.org/applying_a_theme_to_your_application-t817.html
编辑(从该论坛复制):
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Call setTheme before creation of any(!) View.
setTheme(android.R.style.Theme_Dark);
// ...
setContentView(R.layout.main);
}
Run Code Online (Sandbox Code Playgroud)
TPR*_*eal 58
如果你想改变一个已经存在的活动主题,称recreate()后setTheme().
注意:如果更改主题,请不要调用recreate onCreate(),以避免无限循环.
Yur*_*kov 21
recreate()(如TPReal所述)将仅重新启动当前活动,但之前的活动仍将在后台堆栈中,并且主题将不会应用于它们.
因此,此问题的另一个解决方案是完全重新创建任务堆栈,如下所示:
TaskStackBuilder.create(getActivity())
.addNextIntent(new Intent(getActivity(), MainActivity.class))
.addNextIntent(getActivity().getIntent())
.startActivities();
Run Code Online (Sandbox Code Playgroud)
编辑:
在UI或其他地方执行主题更改后,只需输入上面的代码即可.您的所有活动都应该具有setTheme()之前调用的方法onCreate(),可能在某些父活动中.这也是存储所选主题SharedPreferences,读取然后使用setTheme()方法设置的常规方法.
Unk*_*own 16
我遇到了同样的问题,但我找到了解决方案.
public class EditTextSmartPhoneActivity extends Activity implements DialogInterface.OnClickListener
{
public final static int CREATE_DIALOG = -1;
public final static int THEME_HOLO_LIGHT = 0;
public final static int THEME_BLACK = 1;
int position;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
position = getIntent().getIntExtra("position", -1);
switch(position)
{
case CREATE_DIALOG:
createDialog();
break;
case THEME_HOLO_LIGHT:
setTheme(android.R.style.Theme_Holo_Light);
break;
case THEME_BLACK:
setTheme(android.R.style.Theme_Black);
break;
default:
}
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
private void createDialog()
{
/** Options for user to select*/
String choose[] = {"Theme_Holo_Light","Theme_Black"};
AlertDialog.Builder b = new AlertDialog.Builder(this);
/** Setting a title for the window */
b.setTitle("Choose your Application Theme");
/** Setting items to the alert dialog */
b.setSingleChoiceItems(choose, 0, null);
/** Setting a positive button and its listener */
b.setPositiveButton("OK",this);
/** Setting a positive button and its listener */
b.setNegativeButton("Cancel", null);
/** Creating the alert dialog window using the builder class */
AlertDialog d = b.create();
/** show dialog*/
d.show();
}
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
AlertDialog alert = (AlertDialog)dialog;
int position = alert.getListView().getCheckedItemPosition();
finish();
Intent intent = new Intent(this, EditTextSmartPhoneActivity.class);
intent.putExtra("position", position);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
Fra*_*ani 10
我有类似的问题,我这样解决了..
@Override
public void onCreate(Bundle savedInstanceState) {
if (getIntent().hasExtra("bundle") && savedInstanceState==null){
savedInstanceState = getIntent().getExtras().getBundle("bundle");
}
//add code for theme
switch(theme)
{
case LIGHT:
setTheme(R.style.LightTheme);
break;
case BLACK:
setTheme(R.style.BlackTheme);
break;
default:
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//code
}
Run Code Online (Sandbox Code Playgroud)
此代码用于重新创建活动保存包并更改主题.你必须编写自己的onSaveInstanceState(Bundle outState); 从API-11开始,您可以使用recreate()方法
Bundle temp_bundle = new Bundle();
onSaveInstanceState(temp_bundle);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("bundle", temp_bundle);
startActivity(intent);
finish();
Run Code Online (Sandbox Code Playgroud)
代替
getApplication().setTheme(R.style.BlackTheme);
Run Code Online (Sandbox Code Playgroud)
用
setTheme(R.style.BlackTheme);
Run Code Online (Sandbox Code Playgroud)
我的代码:在 onCreate() 方法中:
super.onCreate(savedInstanceState);
if(someExpression) {
setTheme(R.style.OneTheme);
} else {
setTheme(R.style.AnotherTheme);
}
setContentView(R.layout.activity_some_layout);
Run Code Online (Sandbox Code Playgroud)
某处(例如,单击按钮时):
YourActivity.this.recreate();
Run Code Online (Sandbox Code Playgroud)
您必须重新创建活动,否则 - 不会发生更改
| 归档时间: |
|
| 查看次数: |
163249 次 |
| 最近记录: |