我知道有一种方法可以在按钮点击时更改应用程序的默认主题。Blackmart开发人员已经做到了这一点。我已经像搜索了1000页Google,但我只找到了这个(不起作用)。
getApplication().setTheme(Theme.Holo)
我已经在res/values/styles.xml中创建了新的样式,还有其他动态更改它的方法吗?甚至重新启动应用程序?
以下博客可以解决您的问题:
http://mrbool.com/how-to-change-the-layout-theme-of-an-android-application/25837
以下是博客中的代码,方便参考:
假设您已经在XML文件中定义了以下三个主题:R.style.FirstTheme
、R.style.SecondTheme
和 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;
}
}
}
让我们在“Utils”文件中编写以下代码:
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;
}
}
}
希望能帮到你...public AlertDialog create() {
final AlertDialog dialog = new AlertDialog(P.mContext);
P.apply(dialog.mAlert);
[...]
}
调用了AlertDialog的“非主题感知”构造函数,它看起来像这样:
protected AlertDialog(Context context) {
this(context, com.android.internal.R.style.Theme_Dialog_Alert);
}
AlertDialog有第二个构造函数,可用于更改主题:
protected AlertDialog(Context context, int theme) {
super(context, theme);
[...]
}
建造者只是没有调用它。
查看以下帖子以获取更多相关修复..
以下是得票最多的答案:
new AlertDialog.Builder(
new ContextThemeWrapper(context, android.R.style.Theme_Dialog))
ListView
жңүд»Җд№Ҳй—®йўҳеҗ—пјҹжӮЁиғҪеҗҰдҪҝз”ЁжӯӨж–№жі•жӣҙж”№йҷӨListView
д№ӢеӨ–зҡ„жүҖжңүе…¶д»–и§Ҷеӣҫзҡ„дё»йўҳпјҹ - Praful BhatnagarAlertDialog
问题进行了修改。 - Praful BhatnagarchangeToTheme(Activity activity, int theme)
替换为 changeToTheme(AppCompatActivity activity, int theme)
然后您可以使用以下代码:
activity.recreate()
- Karue Benson Karue
setTheme()
后又调用了setContentView(View)
吗? - Praful BhatnagarsetTheme()
之后重新启动该活动,看看是否有效。 - Praful Bhatnagar