日夜模式切换
<h3><center>日夜模式切换</center></h3>
<h4>接入方式</h4>
<pre><code>导入`skin-lib-release.aar`;地址:[smb://192.168.158.238/share/移动组/android/公共组件/日夜切换库]
build.gradle中配置:` implementation fileTree(include: ['*.aar'], dir: 'libs')`</code></pre>
<h4>使用方式</h4>
<p>1.values目录下新建skin.xml;放入日夜切换的id及theme配置;
<img src="https://www.showdoc.cc/server/api/common/visitfile/sign/df0d02884c95bb1198b9f122aaf2fe58?showdoc=.jpg" alt="" /></p>
<p><img src="https://www.showdoc.cc/server/api/common/visitfile/sign/62175870ee2a63c139c10d94f9cab9ef?showdoc=.jpg" alt="" /></p>
<p>2.在xml中使用
<img src="https://www.showdoc.cc/server/api/common/visitfile/sign/0051fc078eba5347fff2eab5d896babd?showdoc=.jpg" alt="" /></p>
<p>3.Application中初始化</p>
<pre><code> @Override
public void onCreate() {
super.onCreate();
SkinEngine.changeSkin(SkinPrefUtils.isNightModel(this)?R.style.NightTheme:R.style.DayTheme);
....省略其他代码.....
}</code></pre>
<ol>
<li>
<p>Activity中配置:</p>
<pre><code>@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getLayoutInflater();
this.mLayoutInfalter.applyCurrentSkin();
AppCompatDelegate delegate = this.getDelegate();
if (delegate instanceof LayoutInflater.Factory2) {
this.mLayoutInfalter.setFactory2((LayoutInflater.Factory2) delegate);
}
.....省略其他代码.....
}
/**
* 重写getLayoutInflater方法
* @return
*/
@Override
public final LayoutInflater getLayoutInflater() {
if (this.mLayoutInfalter == null) {
this.mLayoutInfalter = new SkinLayoutInflater(this);
}
return this.mLayoutInfalter;
}</code></pre>
<p>3.Fragment中配置:</p>
<pre><code>@Override
public void onDetach() {
super.onDetach();
LayoutInflater layoutInflater = this.getLayoutInflater();
if (layoutInflater instanceof SkinLayoutInflater) {
SkinLayoutInflater skinLayoutInflater = (SkinLayoutInflater)layoutInflater;
skinLayoutInflater.destory();
}
}</code></pre>
<p>4.配置完成,在需要切换的地方处理:</p>
<pre><code> SkinPrefUtils.setNightModel(getContext(), !SkinPrefUtils.isNightModel(getContext()));
SkinEngine.changeSkin(SkinPrefUtils.isNightModel(getContext())? R.style.NightTheme : R.style.DayTheme);</code></pre>
<h4>5.自定义控件配置日夜切换模式</h4>
<p>(1) 在xml中配置自定义控件的属性
<img src="https://www.showdoc.cc/server/api/common/visitfile/sign/96077f761cccb800fee67dab9a8cb0d6?showdoc=.jpg" alt="" /></p>
</li>
</ol>
<p>(2) 在Application中声明需要切换的控件</p>
<pre><code> @Override
public void onCreate() {
super.onCreate();
...省略其他代码....
/**
* 参数1:对应自己的自定义控件
* 参数2:自定义的日夜模式切换的配置类(名称自己定义的)
*/
SkinEngine.registerSkinApplicator(CustomView.class, new SkinCustomViewApplicator());
}</code></pre>
<p>(3)配置自定义控件切换的配置类</p>
<pre><code>public class SkinCustomViewApplicator extends SkinViewApplicator {
public SkinCustomViewApplicator() {
super();
/**
*改变的是lineColor属性颜色值
*/
addAttributeApplicator("lineColor", new IAttributeApplicator<CustomView>() {
@Override
public void onApply(CustomView view, TypedArray typedArray, int typedArrayIndex) {
/**
*这里的代码就是自己写的自定义控件中需要改变颜色的一个方法
*/
view.setLineColor(typedArray.getColor(typedArrayIndex, Color.BLACK));
}
});
}
}</code></pre>
<h5>PS:view.setLineColor(typedArray.getColor(typedArrayIndex, Color.BLACK));对应的就是自定义控件中的这个方法(开发者自己写的方法)</h5>
<p><img src="https://www.showdoc.cc/server/api/common/visitfile/sign/b4fff87b3f93916ea965ce788dea34dd?showdoc=.jpg" alt="" /></p>