Android开发Android开发Android技术知识

Android 切换主题风格(Theme换肤效果)

2019-05-15  本文已影响5人  Kandy_JS

参考

1、Android 切换主题以及换肤的实现

截图

1、默认打开


image.png

2、点击【换主题色】


image.png

需知

代码

1、清单文件manifest中,app默认使用AppTheme(或者自定义Theme)
<application
        ...
        android:theme="@style/AppTheme">
        ...
</application>
2、style.xml 风格:
<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <!--自定义属性-->
        <item name="myBgColor">@color/wx_bg_gray</item>
        <item name="myButtonHeight">60dp</item>
    </style>


    <style name="MyTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/sysColorPrimary</item>
        <item name="colorPrimaryDark">@color/sysColorPrimaryDark</item>
        <item name="colorAccent">@color/sysColorAccent</item>
        <item name="actionBarSize">80dp</item>
        <item name="colorButtonNormal">@color/wx_green</item>

        <!--自定义属性-->
        <item name="myBgColor">@color/wx_sunset</item>
        <item name="myButtonHeight">100dp</item>
    </style>

    <!--button style-->
    <style name="MyBtnStyle">
        <item name="android:layout_width">match_parent</item>
        <item name="android:layout_height">?attr/myButtonHeight</item>
    </style>

</resources>

3、attr.xml 自定义属性(一般做自定义View的人肯定熟悉):
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="myBgColor" format="color" />
    <attr name="myButtonHeight" format="dimension" />
</resources>
4、layout.xml 布局文件使用
<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"
    tools:context=".MainActivity"
    android:orientation="vertical"
    android:background="?attr/myBgColor">

    <TextView
        android:id="@+id/sample_text"
        android:textSize="25sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        />

    <EditText
        android:id="@+id/et_1"
        android:textSize="25sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Edit it"/>

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CheckBox"
        android:checked="true"/>

    <Button
        android:id="@+id/btn_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="点击这里"/>

    <Button
        android:id="@+id/btn_2"
        style="@style/MyBtnStyle"
        android:text="换主题色"/>

</LinearLayout>
5、代码控制切换主题
//切换不同的风格,必须在setContentView之前做
        if(useMyTheme){
            setTheme(R.style.MyTheme);
        }else{
            setTheme(R.style.AppTheme);
        }

        setContentView(R.layout.activity_main);


        btn_2 = findViewById(R.id.btn_2);
        btn_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                useMyTheme = !useMyTheme;
                recreate(); //重启画面
            }
        });
上一篇 下一篇

猜你喜欢

热点阅读