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

更多相关文章

  1. Android(安卓)ProgressBar设置转圈样式
  2. Android(安卓)RatingBar:评价条控件
  3. Android(安卓)studio 原生自带下拉加载控件SwipeRefreshLayout使
  4. Android中测量控件的内部文字内容的宽、高度
  5. 【读书笔记-《Android游戏编程之从零开始》】4.Android(安卓)游
  6. Android(安卓)ConstraintLayout使用指南
  7. android-HttpClient上传信息(包括图片)到服务端
  8. Android(安卓)获取控件高度宽度三种方法,防止0的出现~~~~赞
  9. [Android] Viewpager 与 Google map v2 冲突,导致地图不能左右滑

随机推荐

  1. Android应用程序签名
  2. Linux与Android
  3. Android智能推荐、MVP架构电商应用、mark
  4. Android(安卓)GUI系统框架介绍
  5. android是什么?学 android开发能做什么?
  6. Android中JNI的使用
  7. Android客户端程序员的一些思考
  8. Android多进程之Binder的意外死亡及权限
  9. Android(安卓)创建与解析XML(六)—— 比较
  10. 【Android(安卓)笔记 五】 Android(安卓)