【Android开发基础】Android软件换肤功能(AppTheme)实现一个用于改变文字颜色的上下文菜单

该文章详细介绍了如何在Android应用中实现换肤功能,主要涉及AppTheme的使用,通过编辑styles.xml文件创建不同颜色主题,如红色、蓝色和黄色。利用SharedPreferences存储用户选择的主题,以便在应用启动时恢复。文章提供了两个Activity的示例,一个是主界面,另一个是主题切换界面,用户可以在切换界面中选择不同的背景颜色和文字颜色,点击后会切换回主界面并应用所选主题。
摘要由CSDN通过智能技术生成

一、简述

  • 描述:在很多软件中拥有换肤功能,包括边框图片切换,颜色切换,字体颜色、样式、大小等,其中尤为常见的就是小说软件的背景色切换。实现一个用于改变文字颜色、背景的上下文菜单。
  • 难度:初级
  • 知识点:
  • 1、AppTheme(styles.xml)的使用
  • 2、SharedPreferences存储应用

软件换肤Theme

二、AppTheme设计

AppTheme界面风格设计,主要就是对res -> values -> styles.xml文件进行编辑

	<!-- 默认风格 -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <!--  红色风格  -->
    <style name="AppThemeRed" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- 全局文字颜色 -->
        <item name="android:textColor">@color/red</item>
        <item name="android:colorBackground">@color/redBack</item>
    </style>
    <!--  蓝色风格  -->
    <style name="AppThemeBlue" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- 全局文字颜色 -->
        <item name="android:textColor">@color/blue</item>
        <item name="android:colorBackground">@color/blueBack</item>
    </style>
    <!--  黄色风格  -->
    <style name="AppThemeYellow" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- 全局文字颜色 -->
        <item name="android:textColor">@color/yellow</item>
        <item name="android:colorBackground">@color/yellowBack</item>
    </style>

三、具体实现

需要两个XXActivity

1、Activity1

(1)activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/l1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right">

        <TextView
            android:id="@+id/test"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:paddingTop="10dp"
            android:paddingRight="10dp"
            android:textSize="25dp"
            android:text="主界面"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/l1"
        android:layout_marginTop="20dp"
        android:orientation="vertical"
        android:background="#F0F0F0"
        android:paddingLeft="50dp">

        <TextView
            android:id="@+id/text001"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Android开发-云端new守夜人"
            android:textSize="20dp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Android开发-云端new守夜人"
            android:textSize="20dp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Android开发-云端new守夜人"
            android:textSize="20dp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Android开发-云端new守夜人"
            android:textSize="20dp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Android开发-云端new守夜人"
            android:textSize="20dp"/>

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Android开发-云端new守夜人"
            android:textSize="20dp"/>

    </LinearLayout>

    <!--    图层二:切换图层三按钮    -->
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="right|bottom">

        <Button
            android:id="@+id/qh"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginBottom="50dp"
            android:layout_marginRight="50dp"
            android:text=""
            android:background="@drawable/shape_circular"/>

    </LinearLayout>

</RelativeLayout>
(1)MainActivity.java
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    Button qh;
    TextView text001;
    SharedPreferences init;  // 界面风格存储,方便下次打开不会刷新

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	// 初始化
        init();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 绑定事件
        onclick();
    }

    private void init() {
        init = super.getSharedPreferences("init", MODE_PRIVATE);
        if ("red".equals(init.getString("theme", ""))){  // 判断当前风格是什么
            setTheme(R.style.AppThemeRed);
        } else if ("blue".equals(init.getString("theme", ""))) {
            setTheme(R.style.AppThemeBlue);
        } else if ("yellow".equals(init.getString("theme", ""))) {
            setTheme(R.style.AppThemeYellow);
        } else if ("mr".equals(init.getString("theme", ""))) {
            setTheme(R.style.AppTheme);
        }
    }

    private void onclick() {
        qh = findViewById(R.id.qh);
        text001 = findViewById(R.id.text001);
        qh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, QhThemeActivity.class);
                startActivity(intent);
            }
        });
    }
}

1、Activity2

(1)activity_qh_theme.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/l1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="right">

        <TextView
            android:id="@+id/test"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:paddingTop="10dp"
            android:paddingRight="10dp"
            android:textSize="25dp"
            android:text="切换软件皮肤"/>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/l1"
        android:layout_marginTop="20dp"
        android:orientation="vertical"
        android:paddingLeft="50dp">

        <Button
            android:id="@+id/red"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="红色背景"/>

        <Button
            android:id="@+id/blue"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="蓝色背景"/>

        <Button
            android:id="@+id/yellow"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="黄色背景"/>

        <Button
            android:id="@+id/mr"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="默认背景"/>

    </LinearLayout>

</RelativeLayout>
(2)QhThemeActivity.java
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class QhThemeActivity extends AppCompatActivity implements View.OnClickListener {

    Button blue, red, yellow, mr;
    SharedPreferences init;   // 存储风格

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    	// 初始化
        init();
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qh_theme);
        // 绑定事件
        onclick();
    }

    private void onclick() {
        blue = findViewById(R.id.blue);
        red = findViewById(R.id.red);
        yellow = findViewById(R.id.yellow);
        mr = findViewById(R.id.mr);
        blue.setOnClickListener(this);
        red.setOnClickListener(this);
        yellow.setOnClickListener(this);
        mr.setOnClickListener(this);
    }

    private void init() {
        init = super.getSharedPreferences("init", MODE_PRIVATE);
        if ("red".equals(init.getString("theme", ""))){  // 判断当前风格是什么
            setTheme(R.style.AppThemeRed);
        } else if ("blue".equals(init.getString("theme", ""))) {
            setTheme(R.style.AppThemeBlue);
        } else if ("yellow".equals(init.getString("theme", ""))) {
            setTheme(R.style.AppThemeYellow);
        } else if ("mr".equals(init.getString("theme", ""))) {
            setTheme(R.style.AppTheme);
        }
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        SharedPreferences.Editor editor = init.edit();
        switch (v.getId()) {
            case R.id.blue:
                editor.putString("theme", "blue");
                break;
            case R.id.red:
                editor.putString("theme", "red");
                break;
            case R.id.yellow:
                editor.putString("theme", "yellow");
                break;
            case R.id.mr:
                editor.putString("theme", "mr");
                break;
            default:
                break;
        }
        editor.commit();
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}

四、源代码

Android软件换肤功能(AppTheme)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

云端new守夜人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值