演示效果
Usage xml
android:background=
"?attr/zzbackground"
app:backgroundAttr="zzbackground"
java
@Override
protected void onCreate(Bundle savedInstanceState) {
详细操作描述
第一步:自定义属性
<?xml version="1.0" encoding="utf-8"?>
<resources>
<attr name="zzbackground" format="color|reference"/> <attr name="zzbackgroundDrawable" format="reference"/> <attr name="zztextColor" format="color"/> <attr name="zzItemBackground" format="color"/> </resources>
第二步:配置夜间style文件
<resources>
为相关属性设置对应模式的属性值:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="dayBackground">#F2F4F7</color> <color name="dayTextColor">#000</color> <color name="dayItemBackground">#fff</color> <color name="nightItemBackground">#37474F</color> <color name="nightBackground">#263238</color> <color name="nightTextColor">#fff</color> </resources>
第三步:在布局文件中配置使用对应属性
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:background="?attr/zzbackground" app:backgroundAttr="zzbackground" tools:context="com.thinkfreely.changemode.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:backgroundAttr="colorPrimary" app:titleTextColor="?attr/zztextColor" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <Button android:layout_width="match_parent" android:layout_height="120dp" android:gravity="center" android:textColor="?attr/zztextColor" app:textColorAttr="zztextColor" android:background="?attr/zzItemBackground" app:backgroundAttr="zzItemBackground" android:padding="10dp" android:layout_marginBottom="8dp" android:textSize="22sp" android:textAllCaps="false" android:text="夜间模式切换by Mr.Zk" /> <android.support.v7.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" android:scrollbars="vertical"/> </LinearLayout>
注意textColorAttr、backgroundAttr、backgroundDrawableAttr三个属性。如需当前页面立即刷新,需填加相应属性。
属性 描述
textColorAttr 修改字体颜色时设置。如R.attr.zztextColor 传zztextColor 即可。例:app:textColorAttr="zztextColor" backgroundAttr 修改背景颜色/背景图片时设置。同上。例: app:backgroundAttr="zzbackground" backgroundDrawableAttr 修改背景颜色/背景图片时设置。同上。例: app:backgroundDrawableAttr="zzbackground"
第四步:页面调用java代码
@Override
protected void onCreate(Bundle savedInstanceState) {
代码调用三步,即可开始夜间之旅。 如果页面有新创建的视图要加入夜间模式控制,安卓源码调用:
如果在改变夜间模式时有其他非标准定义的属性时,可在ChangeModeController.changeDay或ChangeModeController.changeNight之后调用如下代码给相关属性赋值:
TypedValue attrTypedValue = ChangeModeController.getAttrTypedValue(
this, R.attr.zztextColor);
toolbar.setTitleTextColor(getResources().getColor(attrTypedValue.resourceId));
源码下载:http://www.codesocang.com/kj/transition/33588.html
转载请注明原文地址: https://ju.6miu.com/read-1122649.html