Android主题定制

在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的孩子节点,以新增控件的显示样式。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值