深入解析Android declare-styleable attr style theme(下)

概述

Android5.0之前,我们只能给Application或者Activity设置theme,5.0之后引入Material Design涉及,View也可以设置theme了。
其子View也会继承这个Theme,如ToolBar的theme

        <android.support.v7.widget.Toolbar  android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/AppTheme.AppBarOverlay" app:popupTheme="@style/AppTheme.PopupOverlay"/>

其中 app:popupTheme是指定弹出的菜单的样式,比如ActionBar’是黑色的,但是溢出菜单要求是白底黑字
可以使用app:popupTheme="ThemeOverlay.AppCompat.Light"

注意:android:theme这个属性,在android3.0以上可以应用到View及其子View,
但是在android3.0以下只能应用到当前的View,不能使子View起作用的,所有要用到该theme的View都要加上

原理

在android 5.0之前,如果我们想要设置 View 独特于主题的style,比如Theme.Holo.Light.DarkActionBar
白色主题下的黑色ActionBar,是通过主题下的item来设置

<style name="Theme.Holo.Light.DarkActionBar"> <item name="android:actionBarWidgetTheme">@android:style/Theme.Holo</item> </style>

主要通过新加入item来覆盖原有style,底层是通过ContextThemeWrapper来装饰的,如ThemeOverlay.AppCompat.Dark.ActionBar
另外,5.0引入了一个新的theme属性colorEdgeEffect,用于指定View的过度滑动颜色。我们可以在values-v21中新建style.xml
加入如下属性来改变View 过渡效果。

<item name="android:colorEdgeEffect">@android:color/holo_red_dark</item>

总结

  • Theme 和Style本质都是改变界面的样式的,都是以style的方式定义,只是theme是window级别的(5.0以上可以用到View上),style是view级别的
  • Theme中可以指定单个View的Style
<style name="Theme.PageIndicatorDefaults" parent="android:Theme"> <item name="vpiIconPageIndicatorStyle">@style/Widget.IconPageIndicator</item> <item name="vpiTabPageIndicatorStyle">@style/Widget.TabPageIndicator</item> </style>

你可能感兴趣的:(android,style,theme)