Android:换肤框架Android-Skin-Support

gihub地址: https://github.com/ximsfei/Android-skin-support

样例:

默认:

更换后:

一、引入依赖:

复制代码
// -- 换肤依赖
    implementation 'skin.support:skin-support:4.0.5'// skin-support
    implementation 'skin.support:skin-support-appcompat:4.0.5'// skin-support 基础控件支持
    implementation 'skin.support:skin-support-design:4.0.5'// skin-support-design material design 控件支持[可选]
    implementation 'skin.support:skin-support-cardview:4.0.5'// skin-support-cardview CardView 控件支持[可选]
    implementation 'skin.support:skin-support-constraint-layout:4.0.5'// skin-support-constraint-layout ConstraintLayout 控件支持[可选]

二、创建MyApplication:

复制代码
public class Myapplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        SkinCompatManager.withoutActivity(this)
                .addInflater(new SkinAppCompatViewInflater())           // 基础控件换肤初始化
                .addInflater(new SkinMaterialViewInflater())            // material design 控件换肤初始化[可选]
                .addInflater(new SkinConstraintViewInflater())          // ConstraintLayout 控件换肤初始化[可选]
                .addInflater(new SkinCardViewInflater())                // CardView v7 控件换肤初始化[可选]
                .setSkinStatusBarColorEnable(false)                     // 关闭状态栏换肤,默认打开[可选]
                .setSkinWindowBackgroundEnable(false)                   // 关闭windowBackground换肤,默认打开[可选]
                .loadSkin();


    }


}

AndroidManifest.xml配置MyApplication.java:

三、创建需要换肤的资源

(1)选中main -> 右键New->Directory 创建res-后缀名

(2)res-后缀名资源文件下创建对应的drawable、values等资源文件,如下图所示

以res-red资源为例:

shape_my_info_bg_red.xml:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient
        android:angle="90"
        android:endColor="#E91E63"
        android:startColor="#70E91E63" />
</shape>

colors.xml:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <color name="skin_topbar_text_color_red">#860F08</color>
    <color name="skin_button_color_red">#CA493F</color>
</resources>

四、初始布局使用默认res文件下资源

复制代码
<?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"
    android:orientation="vertical"
    android:background="@drawable/shape_my_info_bg"
    tools:context=".MainActivity">

    <!--标题-->
    <TextView
        android:id="@+id/topBarTv"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center"
        android:textStyle="bold"
        android:textSize="20sp"
        android:text="标题信息"
        android:textColor="@color/skin_topbar_text_color"/>

    <Button
        android:id="@+id/changeBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:background="@drawable/shape_my_info_bg"
        android:textStyle="bold"
        android:textColor="@color/skin_button_color"
        android:text="一键换肤(默认颜色)"/>

    <Button
        android:id="@+id/changeRedBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:background="@drawable/shape_my_info_bg"
        android:textStyle="bold"
        android:textColor="@color/skin_button_color"
        android:text="一键换肤(红色)"/>


    <Button
        android:id="@+id/changeYellowBtn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:background="@drawable/shape_my_info_bg"
        android:textStyle="bold"
        android:textColor="@color/skin_button_color"
        android:text="一键换肤(黄色)"/>





</LinearLayout>

五、使用

复制代码
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
 
    // 换肤按钮
    // 默认皮肤
    private Button changeBtn;
    // 红色皮肤
    private Button changeRedBtn;
    // 黄色皮肤
    private Button changeYellowBtn;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initControls();

    }


    // 控件初始化
    private void initControls(){
        // 默认颜色
        changeBtn = findViewById(R.id.changeBtn);
        changeBtn.setOnClickListener(this);
        // 红色
        changeRedBtn = findViewById(R.id.changeRedBtn);
        changeRedBtn.setOnClickListener(this);
        // 黄色
        changeYellowBtn = findViewById(R.id.changeYellowBtn);
        changeYellowBtn.setOnClickListener(this);

    }

    /**
     * 点击事件
     * */
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.changeBtn:// 默认颜色
                // 恢复默认皮肤
                SkinCompatManager.getInstance().restoreDefaultTheme();
                break;
            case R.id.changeRedBtn:// 红色
                // 前缀加载
                //SkinCompatManager.getInstance().loadSkin("light", SkinCompatManager.SKIN_LOADER_STRATEGY_PREFIX_BUILD_IN);
                // 后缀加载
                SkinCompatManager.getInstance().loadSkin("red", null,SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN);
                break;
            case R.id.changeYellowBtn:// 黄色
                // 前缀加载
                //SkinCompatManager.getInstance().loadSkin("light", SkinCompatManager.SKIN_LOADER_STRATEGY_PREFIX_BUILD_IN);
                // 后缀加载
                SkinCompatManager.getInstance().loadSkin("yellow", null,SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN);
                break;
        }
    }

    /**
     * 如果项目中使用的Activity继承自AppCompatActivity,需要重载getDelegate()方法
     * */
    @NonNull
    @Override
    public AppCompatDelegate getDelegate() {
        return SkinAppCompatDelegateImpl.get(this, this);
    }



}
相关推荐
hunterandroid23 分钟前
Android 线上卡顿治理:从布局层级到主线程负载的全链路排查
android
恋猫de小郭1 小时前
Dart Skills CLI 1.0 :AI 时代的 Dart 交付支持
android·前端·flutter
2501_916007471 小时前
使用Apple Dashboards显示和自定iOS应用性能指标与数据可视化指南
android·ios·小程序·https·uni-app·iphone·webview
蜡台2 小时前
# 已解决|MySQL 8\.4\.11 密码正确仍报错1045 \(28000\) Access denied 终极修复方案
android·mysql·adb
JMchen1232 小时前
2026年六款主流AI编程工具深度实测:Cursor、Copilot、Claude Code等对比与选型思考
android·kotlin·copilot·ai编程·开发工具·cursor·claude code
mmsx2 小时前
osmdroid 地图实战 05|让地图动起来:三个实时能力 + 六条工程排雷清单
android
码农coding2 小时前
android12 SystemUI组件之StatusBar启动
android
hai_android3 小时前
Kotlin 协程上下文:从 plus 到 CombinedContext,彻底搞懂左偏结构
android·kotlin
淡淡的香烟3 小时前
AndroidKMP之网络请求
android·网络
basketball6163 小时前
AI Infra 推理部署技术总结:2. vLLM 内核——PagedAttention 与调度器原理
android·人工智能·vllm·ai infra