以编程方式更改活动主题不起作用[重复]

问题描述 投票:0回答:3

这个问题在这里已有答案:

我想从代码更改应用程序主题,但它不起作用。

   @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setTheme(R.style.NightMode);

    setContentView(R.layout.activity_main);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar)

错误

E / AndroidRuntime:FATAL EXCEPTION:main进程:com.w7orld.animex,PID:23421 java.lang.RuntimeException:无法启动活动ComponentInfo {com.w7orld.animex / com.w7orld.animex.MainActivity}:java.lang。 IllegalStateException:此Activity已经有一个由窗口装饰提供的操作栏。不要在主题中请求Window.FEATURE_SUPPORT_ACTION_BAR并将windowActionBar设置为false以使用工具栏。在android.app.A活动中的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)处于android.app.A活动中的android.app.A活动(活动传递:动作:动态:0)处于android.app.A活动。 ActivityThread $ H.handleMessage(ActivityThread.java:1589)位于android.app.AtoT.Thread.main的android.os.Handler.dispatchMessage(Handler.java:106)android.os.Looper.loop(Looper.java:164) (ActivityThread.java:6494)位于com.android.internal.os的com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)的java.lang.reflect.Method.invoke(Native Method) .ZygoteInit.main(ZygoteInit.java:807)引起:java.lang.IllegalStateException:此Activity已经有一个由窗口装饰提供的操作栏。不要在主题中请求Window.FEATURE_SUPPORT_ACTION_BAR并将windowActionBar设置为false以使用工具栏。在android.support.v7.app.AppCompatDelegateImpl.setSupportActionBar(AppCompatDelegateImpl.java:345)的android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)com.w7orld.animex.MainActivity.onCreate(MainActivity) .java:70)在android.app.Activity.performCreate(Activity.java:7009)的android.app.Activity.performCreate(Activity.java:7009)android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)在android.app.A活动中的android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)处于android.app.A活动中的android.app.A活动,活动(动作,玩具:来自0:)。 ActivityThread $ H.handleMessage(ActivityThread.java:1589)位于android.app.AtoT.Thread.main的android.os.Handler.dispatchMessage(Handler.java:106)android.os.Looper.loop(Looper.java:164) (ActivityThread.java:6494)位于com.android.internal.os.RuntimeInit的$ java.lang.reflect.Method.invoke(Native Method)$ MethodAndArgsCal ler.run(RuntimeInit.java:438)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

android android-activity android-theme runtimeexception
3个回答
0
投票

我找到了这样做的方法。在onCreate我把它

setTheme(Designs.getThemeNoActionBar(this));
    setContentView(R.layout.activity_main);

从共享首选项中获取主题并返回主题而不使用操作栏。

  public static int getThemeNoActionBar(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("Designs", Context.MODE_PRIVATE);
        int theme = sharedPreferences.getInt("theme", R.style.AppTheme);
        if (theme == nightModeTheme)
            return R.style.NightMode_NoActionBar;
        else if (theme == theme1)
            return R.style.Theme1_NoActionBar;
        else 
             return R.style.AppTheme_NoActionBar;
    }

在风格res

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="NightMode.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

<style name="Theme1.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

0
投票

我们设置夜间模式如下:

public class App extends Application {
 public static final String TAG = "App";

private boolean isNightModeEnabled = false;

 @Override
   public void onCreate() {
   super.onCreate();

  // We load the Night Mode state here
   SharedPreferences mPrefs =  PreferenceManager.getDefaultSharedPreferences(this);
   this.isNightModeEnabled = mPrefs.getBoolean(“NIGHT_MODE”, false);
 }

public boolean isNightModeEnabled() {
   return isNightModeEnabled;
 }

public void setIsNightModeEnabled(boolean isNightModeEnabled) {
   this.isNightModeEnabled = isNightModeEnabled;
  }
}

由于此实例将在其他所有内容之前启动,因此您可以在需要时调用isNightModeEnabled(),因此,在打开应用程序后,可以在任何Activity中调用。

public final class FeedActivity extends AppCompatActivity {
 private final static String TAG = “FeedActivity”;

@Override
 protected void onCreate(Bundle savedInstanceState) {
    if (MyApplication.getInstance().isNightModeEnabled()) {
       setTheme(R.style.FeedActivityThemeDark);
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_feed);
 }
}

如何在Android中使用夜间模式是posted here

以下是一些示例图片,显示了骑自然光和夜间模式之间的区别:

Light and dark mode


-1
投票

可见的第一个问题是这些线的放置:

setTheme(R.style.NightMode);

setContentView(R.layout.activity_main);

你必须在setContentView(layout)之后立即放置super.onCreate(savedInstanceState);电话。因为这是生成布局及其视图的代码。

© www.soinside.com 2019 - 2024. All rights reserved.