在Android开发过程中,有个新的需求,需要给APP定制一套可手动变换的应用主题。
1.创建不同控件的样式
style.xml
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
<!--below-->
<style name="AppTheme.NoActionBar.Btn">
<item name="btn_background">@drawable/ic_launcher</item>
</style>
attrs.xml
<declare-styleable name="resource_style">
<attr name="btn_background" format="reference"></attr>
</declare-styleable>
2.给相关控件添加样式
<Button
android:text="@string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/btn_background"
android:layout_below="@id/tv_num"/>
3.添加主题引用
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar.Btn">
当然也可以给整个Application节点添加android:theme节点,值同上
要给该控件添加不同的主题样式,可以继承的方式在第1步中的style文件中,添加
AppTheme.NoActionBar.Btn的孩子节点,以新增控件的显示样式。