温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何在Android中设置theme

发布时间:2021-05-22 17:54:00 来源:亿速云 阅读:518 作者:Leah 栏目:移动开发

今天就跟大家聊聊有关如何在Android中设置theme,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

原因一

错误写法:

<style name="AppTheme.NoActionBar">
  <item name="android:windowActionBar">false</item>
  <item name="android:windowNoTitle">true</item>
  <item name="android:windowDrawsSystemBarBackgrounds">true</item>
  <item name="android:statusBarColor">@android:color/transparent</item>
 </style>

其中AppTheme使用的主题是AppCompat的主题,由于AppCompat主题下的windowActionBar和windowNoTitle的命名方式前都没有android字样,所以报错。

正确写法:

<style name="AppTheme.NoActionBar">
  <item name="windowActionBar">false</item>
  <item name="windowNoTitle">true</item>
  <item name="android:windowDrawsSystemBarBackgrounds">true</item>
  <item name="android:statusBarColor">@android:color/transparent</item>
 </style>

原因二

如果主题设置成有Actionbar的Theme并且没有配:

<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>

也会出这样的错误。

看下源码:

在我们设置toolbar时候: ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);点进源码可以看到源码调用逻辑是:

public void setSupportActionBar(@Nullable Toolbar toolbar) {
  getDelegate().setSupportActionBar(toolbar);
 }

在往下追一步,出真相了:

public void setSupportActionBar(Toolbar toolbar) {
  if (!(mOriginalWindowCallback instanceof Activity)) {
   // Only Activities support custom Action Bars
   return;
  }
  //这里会去判有没有actionbar存在,如果有直接抛异常
  final ActionBar ab = getSupportActionBar();
  if (ab instanceof WindowDecorActionBar) {
   throw new IllegalStateException("This Activity already has an action bar supplied " +
     "by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set " +
     "windowActionBar to false in your theme to use a Toolbar instead.");
  }

  // If we reach here then we're setting a new action bar
  // First clear out the MenuInflater to make sure that it is valid for the new Action Bar
  mMenuInflater = null;

  // If we have an action bar currently, destroy it
  if (ab != null) {
   ab.onDestroy();
  }

  if (toolbar != null) {
   final ToolbarActionBar tbab = new ToolbarActionBar(toolbar,
     ((Activity) mContext).getTitle(), mAppCompatWindowCallback);
   mActionBar = tbab;
   mWindow.setCallback(tbab.getWrappedWindowCallback());
  } else {
   mActionBar = null;
   // Re-set the original window callback since we may have already set a Toolbar wrapper
   mWindow.setCallback(mAppCompatWindowCallback);
  }
  invalidateOptionsMenu();
 }

主要在这里:

//这里会去判有没有actionbar存在,如果有直接抛异常
final ActionBar ab = getSupportActionBar();
  if (ab instanceof WindowDecorActionBar) {
   throw new IllegalStateException("This Activity already has an action bar supplied " +
     "by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set " +
     "windowActionBar to false in your theme to use a Toolbar instead.");
  }

Android是什么

Android是一种基于Linux内核的自由及开放源代码的操作系统,主要使用于移动设备,如智能手机和平板电脑,由美国Google公司和开放手机联盟领导及开发。

看完上述内容,你们对如何在Android中设置theme有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注亿速云行业资讯频道,感谢大家的支持。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI