https://www.jianshu.com/p/b0253de8ac04
This is an article about three popular skin changing schemes. It's very good, but it's not suitable. Here's the way I think about it and happen to produce it:
The first is about text color change of TextView:
Because our APP uses one font in a unified way, the customized TextView has been used in the early stage of the project, so color change is relatively simple:
public class TTFTextView extends AppCompatTextView { private Context context; public TTFTextView(Context context) { super(context); } public TTFTextView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context, attrs); } public TTFTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } private void init(Context context, AttributeSet attrs) { this.context = context; judgeTheme(); } @Override public void setTextColor(int color) { super.setTextColor(color); judgeTheme(); } private void judgeTheme() { switch (ThemeUtil.getTheme()) { case ThemeUtil.BLACK: if (getCurrentTextColor() == context.getResources().getColor(R.color.text_color_light)) { setTextColor(context.getResources().getColor(R.color.theme_black_text_color_light)); } else if (getCurrentTextColor() == context.getResources().getColor(R.color.text_color_hard)) { setTextColor(context.getResources().getColor(R.color.theme_black_text_color_hard)); } else if (getCurrentTextColor() == context.getResources().getColor(R.color.text_color_select)) { setTextColor(context.getResources().getColor(R.color.theme_black_text_color_select)); } break; } }
Next, I used the same method to solve the problem of ImageView replacing images:
For example:
<com.x.x.customview.skinview.SkinImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/icon_normal" />
The idea is to rewrite the ImageView, get the resource id of the picture set by src, and then get the resource name from the resource id, which is the "icon" in xml.
After that, we save a black themed image called theme ﹐ black ﹐ icon ﹐ normal.png. At this time, we add "theme ﹐ black ﹐ to the resource name obtained in the previous step, and then get the resource id of the black themed image through the spelled name, and finally assign it to ImageView to solve the problem, hahaha.~
public class SkinImageView extends AppCompatImageView { private Context context; public SkinImageView(Context context) { super(context); } public SkinImageView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(context, attrs); } public SkinImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs); } private void init(Context context, @Nullable AttributeSet attrs) { this.context = context; if (ThemeUtil.getTheme() == ThemeUtil.BLACK) { if (attrs != null) { int src_resource = attrs.getAttributeResourceValue("http://schemas.android.com/apk/res/android", "src", 0); String resourceName = getResources().getResourceName(src_resource); //Log.d(">>>SkinImage", "resource name = " + resourceName); //Print result resource name = com.x.x:drawable/icon_copy if (!TextUtils.isEmpty(resourceName)) { if (resourceName.contains("/")) { String imageName = resourceName.split("/")[1]; if (!imageName.startsWith("theme_black_")) { int resourceId = getResources().getIdentifier("theme_black_" + imageName, "drawable", context.getPackageName()); //No pictures found return 0 if (resourceId != 0) { setImageResource(resourceId); } } } } } } } @Override public void setImageResource(int resId) { if (ThemeUtil.getTheme() == ThemeUtil.BLACK) { String resourceName = getResources().getResourceName(resId); if (!TextUtils.isEmpty(resourceName)) { if (resourceName.contains("/")) { String imageName = resourceName.split("/")[1]; if (!imageName.startsWith("theme_black_")) { int blackResourceId = getResources().getIdentifier("theme_black_" + imageName, "drawable", context.getPackageName()); if (blackResourceId != 0) { resId = blackResourceId; } } } } } super.setImageResource(resId); }