更改主题不起作用

时间:2014-01-06 17:05:56

标签: android themes android-theme expandablelistadapter

我正在尝试更改主题,选择BaseExpandableListAdapter选项。我可以选择选项,它似乎运行正常。我在Log中有很好的结果。但是主题不会改变。有人可以帮忙吗?我能错过什么?

以下是我MainActivity的一部分:

public class MainActivity extends Activity {

static final int THEME_REQUEST = 1;

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == THEME_REQUEST) {
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            System.out.println("Troca tema: " + bundle.getInt("theme"));
            alteraTema(bundle.getInt("theme"));
        }
    }
}

public void alteraTema(int sTheme) {
    switch (sTheme)
    {
    default:
    case 0:
        System.out.println("Tema BA");
        this.setTheme(R.style.JUCEB);
        this.recreate();
        break;
    case 1:
        System.out.println("Tema ES");
        this.setTheme(R.style.JUCEES);
        this.recreate();
        break;
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (Configuracao.urlApi == null){
        Intent intent = new Intent(MainActivity.this, Configuracao.class);
        startActivityForResult(intent, THEME_REQUEST);
    }

这是我的styles.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android">

<style name="AppBaseTheme" parent="@style/Theme.AppCompat.Light"></style>

<!-- Application theme. -->
<style name="Inicial" parent="AppBaseTheme">
    <item name="android:actionBarStyle">@style/Inicial.ActionBar</item>
</style>

<style name="Inicial.ActionBar" parent="@android:style/Widget.Holo.ActionBar">
    <item name="android:background">@drawable/logo_generico</item>
    <item name="android:icon">@android:color/transparent</item>
    <item name="android:titleTextStyle">@style/NoTitleText</item>
    <item name="android:subtitleTextStyle">@style/NoTitleText</item>
</style>

<style name="JUCEES" parent="AppBaseTheme">
    <item name="android:actionBarStyle">@style/JUCEES.ActionBar</item>
</style>

<style name="JUCEES.ActionBar" parent="@android:style/Widget.Holo.ActionBar">
    <item name="android:background">@drawable/logo_es</item>
    <item name="android:icon">@android:color/transparent</item>
    <item name="android:titleTextStyle">@style/NoTitleText</item>
    <item name="android:subtitleTextStyle">@style/NoTitleText</item>
</style>

<style name="JUCEB" parent="AppBaseTheme">
    <item name="android:actionBarStyle">@style/JUCEB.ActionBar</item>
</style>

<style name="JUCEB.ActionBar" parent="@style/Widget.AppCompat.ActionBar">
    <item name="android:background">@drawable/logo_ba</item>
    <item name="android:icon">@android:color/transparent</item>
    <item name="android:titleTextStyle">@style/NoTitleText</item>
    <item name="android:subtitleTextStyle">@style/NoTitleText</item>
</style>

<style name="NoTitleText">
    <item name="android:textSize">0sp</item>
    <item name="android:textColor">#00000000</item>
</style>

更新1

我做了这些改动:

public void alteraTema(int sTheme) {
    switch (sTheme)
    {
    case 0:
        temaEscolha = (R.style.JUCEB);
        setTheme(temaEscolha);
        System.out.println("Tema BA");
        break;
    case 1:
        temaEscolha = (R.style.JUCEES);
        setTheme(temaEscolha);
        System.out.println("Tema ES");
        break;
    }

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setTheme(temaEscolha);
    setContentView(R.layout.activity_main);

现在我可以为每个活动更改主题,但Main_Activity除外。 它只适用于我第一次更改主题。当我再次尝试改变时,不要工作。

更新2

我这样做了:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == THEME_REQUEST) {
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            System.out.println("Troca tema: " + bundle.getInt("theme"));
            alteraTema(bundle.getInt("theme"));
            startActivity(getIntent());
            finish();
        }
    }
}

在我的Confi.java中,我的选择在哪里,我有这个:

    private void finishWithResult() {
    Intent intent = new Intent();
    intent.putExtra("theme", Selecao);
    setResult(Activity.RESULT_OK, intent);
    finish();
}

解决

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == THEME_REQUEST) {
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            System.out.println("Troca tema: " + bundle.getInt("theme"));
            alteraTema(bundle.getInt("theme"));
            finish();
            startActivity( getIntent() );
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在调用setContentView()之前必须设置主题(请参阅documentation)。设置完成后,您无法通过重新启动活动来更改主题。 所以在你的onActivityResult方法中你需要这些东西:

    startActivity( getIntent() );
    finish();

当然,您必须以某种方式记住所选主题(SharedPreferences或将其放入Intent中)。

另见帖子:https://stackoverflow.com/a/11576546/534471