文章目录
一、简述
- 描述:在很多软件中拥有换肤功能,包括边框图片切换,颜色切换,字体颜色、样式、大小等,其中尤为常见的就是小说软件的背景色切换。实现一个用于改变文字颜色、背景的上下文菜单。
- 难度:初级
- 知识点:
- 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);
}
}