如何在Android中设置theme-创新互联

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

目前成都创新互联已为近1000家的企业提供了网站建设、域名、虚拟主机成都网站托管、企业网站设计、加查网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

原因一

错误写法:


  false
  true
  true
  @android:color/transparent
 

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

正确写法:


  false
  true
  true
  @android:color/transparent
 

原因二

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

false
true

也会出这样的错误。

看下源码:

在我们设置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有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


文章名称:如何在Android中设置theme-创新互联
浏览路径:http://myzitong.com/article/ddoiee.html