android5.1 settings的主题从Holo改为了Material,通过Settings分析一下theme的使用。
主题使用场景
改变整个应用的控件风格。Settings的整体风格,先加载Settings的themes,接着layout覆盖themes的设置,最后在java代码里覆盖layout的设置。
theme和sytle的2种继承方式
theme有2种继承方式,通过“点“继承,如Theme.Material.Light.DarkActionBar继承于Theme.Material.Light。通过parent继承。如果2个同时都有,"点"继承的先运行,即parent继承覆盖“点”继承。
theme和style的属性分类
由于大量的继承以及属性使用了“?attr/xxx“来引用其他属性,所以整个属性排布比较混乱。主要区分方法是先从以Style或Theme结尾的属性去找,再找到在继承关系中最后设置该属性的地方。例如:改变checkbox的字体颜色,在themes_material.xml里找到<style name="Theme.Material.Light" parent="Theme.Light">
<item name="checkboxStyle">@style/Widget.Material.Light.CompoundButton.CheckBox</item>
追溯父类,在styles.xml里最终找到textColor引用textColorPrimaryDisableOnly属性
<style name="Widget.CompoundButton">
<item name="focusable">true</item>
<item name="clickable">true</item>
<item name="textAppearance">?attr/textAppearance</item>
<item name="textColor">?attr/textColorPrimaryDisableOnly</item>
<item name="gravity">center_vertical|start</item>
</style>
<style name="Widget.CompoundButton.CheckBox">
<item name="button">?attr/listChoiceIndicatorMultiple</item>
</style>
至于完整的Style列表可以在最原始的主题Theme中查找。
Settings新UI效果
的android5.1的主题checkbox上有动画效果,从上面的checkbox的button属性使用了btn_check_material_anim.xml,其使用了新的标签
<animated-selector xmlns:android="http://schemas.android.com/apk/res/android">
<transition android:fromId="@+id/off" android:toId="@+id/on">
<animation-list>
<item android:duration="15">
<bitmap android:src="@drawable/btn_check_to_on_mtrl_000" android:tint="?attr/colorControlNormal" />
</item>
而点击事件的水波纹效果在background属性使用了item_background_material.xml,其也使用了新标签。
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">
<item android:id="@id/mask">
<color android:color="@color/white" />
</item>
</ripple>
--