Android动态换肤开源库Colorful发布

                       

最近本人需要用到夜间模式,但是经过一番搜索似乎并没有看到好的开源实现,看到有一个类似的库MultipleTheme,但是需要自定义所有要实现换肤功能的View,在页面较少时感觉比较麻烦(适用于页面较多的App)。而Prism又不支持Theme,且目前已经不再更新,因此这个方案也不能用。当发现现有的解决方案不能很好的解决问题时,往往只能自己实现,因此本人花了点时间简单弄了一个实现该功能的开源库,命名为Colorful。

Colorful是基于Theme,无需重启Activity、无需自定义View,方便的实现日间、夜间模式,github地址为 https://github.com/bboyfeiyu/Colorful

Colorful 动态换肤开源库

基于Theme的Android动态换肤开源库,以简单的方式实现夜间模式。

效果如下:

一、使用方式

1.1 自定义属性

<?xml version="1.0" encoding="utf-8"?><resources>    <!-- 自定义属性 -->    <attr name="root_view_bg" format="reference|color" />    <attr name="btn_bg" format="reference|color" />    <attr name="text_color" format="reference|color" /></resources>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

1.2 在布局中使用自定义属性设置View的背景、文本颜色等属性

activity_main.xml中的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/root_view"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:background="?attr/root_view_bg"    tools:context="com.example.androidthemedemo.MainActivity" >    <TextView        android:id="@+id/textview"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:text="@string/change_theme"        android:textColor="?attr/text_color"        android:textSize="20sp" />    <Button        android:id="@+id/change_btn"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/textview"        android:layout_marginTop="20dp"        android:text="@string/change_theme"        android:textColor="?attr/text_color" />    <Button        android:id="@+id/second_btn"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_below="@id/change_btn"        android:layout_marginTop="20dp"        android:text="@string/sec_act"         />    <ListView        android:id="@+id/listview"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_below="@id/second_btn"        android:layout_marginTop="20dp" /></RelativeLayout>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

例如上述布局中我们将root_view的背景设置为"?attr/root_view_bg",代表它的背景是自定义属性root_view_bg的值,还有Textview和Button的textColor属性设置为"?attr/text_color"

1.3 定义多个Theme

然后在不同的Theme中为这些属性设置不同的值,例如,通常我们有日间和夜间模式两种颜色模式。styles.xml中的完整代码如下:

<resources>    <style name="AppBaseTheme" parent="Theme.AppCompat.Light">    </style>    <!-- Application theme. -->    <style name="AppTheme" parent="AppBaseTheme">    </style>    <!-- 日间主题 -->    <style name="DayTheme" parent="AppTheme">        <item name="root_view_bg">@drawable/bg_day</item>        <item name="btn_bg">@color/white_btn_color</item>        <item name="text_color">@color/black_tx_color</item>    </style>    <!-- 夜间主题 -->    <style name="NightTheme" parent="AppTheme">        <item name="root_view_bg">@drawable/bg_night</item>        <item name="btn_bg">@color/black_btn_color</item>        <item name="text_color">@color/white_tx_color</item>    </style></resources>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

两个主题下为同一个属性设置了不同的值,达到切换主题时修改View的相关属性的目的。例如定义在colors.xml中的颜色值。

<?xml version="1.0" encoding="utf-8"?><resources>    <!-- 日间模式 -->    <color name="white_btn_color">#3BB32E</color>    <color name="black_tx_color">#333333</color>    <!-- 夜间模式 -->    <color name="black_btn_color">#aa7788</color>    <color name="white_tx_color">#f0f0f0</color></resources>
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

1.4 设置要修改的View的属性

下面我们为activity_main.xml中的视图进行换肤设置:

ListView  mNewsListView = (ListView) findViewById(R.id.listview);// 为ListView设置要修改的属性,在这里没有对ListView本身的属性做修改ViewGroupSetter listViewSetter = new ViewGroupSetter(mNewsListView, 0);// 绑定ListView的Item View中的news_title视图,在换肤时修改它的text_color属性listViewSetter.childViewTextColor(R.id.news_title, R.attr.text_color);// 构建Colorful对象Colorful mColorful = new Colorful.Builder(this)        .backgroundDrawable(R.id.root_view, R.attr.root_view_bg) // 设置view的背景图片        .backgroundColor(R.id.change_btn, R.attr.btn_bg) // 设置按钮的背景色        .textColor(R.id.textview, R.attr.text_color) // 设置文本颜色        .setter(listViewSetter)           // 手动设置setter        .create(); 
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

首先我们定义了一个listViewSetter,该Setter用于为ListView的每个Item View中的news_title控件设置文本颜色,文本颜色的值是自定义属性text_color的颜色值。然后构建Colorful对象,并且id分别为change_btn、root_view、textview的控件绑定特定属性值,例如backgroundDrawable(R.id.root_view, R.attr.root_view_bg)代表root_view的背景Drawable为自定义属性root_view_bg的值,textColor(R.id.textview, R.attr.text_color)表示id为textview的TextView控件的文本颜色为R.attr.text_color的值。这些属性都在不同的Theme中有不同的值,因此切换Theme时就会发生变化。然后我们将listViewSetter添加到Colorful对象中,在修改主题时被遍历ListView中的所有Item View,然后修改news_title控件的文本颜色。

1.5 切换主题

最后通过Colorful对象设置主题即可实现切换,代码如下:

boolean isNight = false ;// 切换主题private void changeThemeWithColorful() {    if (!isNight) {        mColorful.setTheme(R.style.DayTheme);    } else {        mColorful.setTheme(R.style.NightTheme);    }    isNight = !isNight;}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

时间仓促,功能简单,希望有时间的同学积极加入,将功能慢慢完善起来!

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值