我知道有一种方法来改变按钮,单击应用程序默认主题。 Blackmart开发商都做到了。我已经搜查谷歌像1000页,但我发现眼前这个(没有工作)
i know there is a way to change the app default theme on button click. Blackmart developers have done it. I’ve already searched Google like 1000 pages but i found just this (not working)
getApplication().setTheme(Theme.Holo)
因为我已经创建了一个新的风格RES /价值/ styles.xml是否有任何其他的方式来动态的改变呢?即使重新启动应用程序?
As i already created a new style in res/values/styles.xml is there any other way to dynamically change it? Even rebooting the application?
以下博客可以解决你的问题:
Following blog can solve your problem:
http://mrbool.com/how-to-change-the-layout-theme-of-an-android-application/25837
复制博客code为快速参考:
Copying the blog code for quick reference:
假设你已经定义以下三个主题XML文件中的 R.style.FirstTheme
, R.style.SecondTheme
和 R.style.ThirdTheme
Assuming that you already defined following three themes in the XML file R.style.FirstTheme
, R.style.SecondTheme
and R.style.ThirdTheme
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class ChangeThemeActivity extends Activity implements OnClickListener
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Utils.onActivityCreateSetTheme(this);
setContentView(R.layout.main);
findViewById(R.id.button1).setOnClickListener(this);
findViewById(R.id.button2).setOnClickListener(this);
findViewById(R.id.button3).setOnClickListener(this);
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
switch (v.getId())
{
case R.id.button1:
Utils.changeToTheme(this, Utils.THEME_DEFAULT);
break;
case R.id.button2:
Utils.changeToTheme(this, Utils.THEME_WHITE);
break;
case R.id.button3:
Utils.changeToTheme(this, Utils.THEME_BLUE);
break;
}
}
}
让我们写下面的code。在utils的文件:
Let us write the below code in the "Utils" file:
import android.app.Activity;
import android.content.Intent;
public class Utils
{
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();
activity.startActivity(new Intent(activity, activity.getClass()));
}
/** 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.FirstTheme);
break;
case THEME_WHITE:
activity.setTheme(R.style.SecondTheme);
break;
case THEME_BLUE:
activity.setTheme(R.style.Thirdheme);
break;
}
}
}
希望它可以帮助...
Hope it helps...
编辑1:
以下是原因 AlertDialog
不采取定制主题:
following is the reason AlertDialog
does not take custom theme:
实施Builder.create()是:
Implementation in Builder.create() is:
public AlertDialog create() {
final AlertDialog dialog = new AlertDialog(P.mContext);
P.apply(dialog.mAlert);
[...]
}
这就要求AlertDialog的非主题意识的构造,它看起来是这样的:
which calls the "not-theme-aware" constructor of AlertDialog, which looks like this:
protected AlertDialog(Context context) {
this(context, com.android.internal.R.style.Theme_Dialog_Alert);
}
还有第二个构造函数AlertDialog用于改变主题:
There is a second constructor in AlertDialog for changing themes:
protected AlertDialog(Context context, int theme) {
super(context, theme);
[...]
}
该建筑工地只是不叫。
that the Builder just doesn't call.
看看下面的帖子更多的相关补丁。
Check out following post for more relevant fixes..
如何更改主题AlertDialog
以下是最投票回答:
new AlertDialog.Builder(
new ContextThemeWrapper(context, android.R.style.Theme_Dialog))