Android 切換主題風格(Theme換膚效果) 參考 截圖 需知 代碼

參考

1、Android 切換主題以及換膚的實現

截圖

1、默認打開


2、點擊【換主題色】


需知

  • 主題色運用:manifest清單文件中application的屬性之一,android:theme="@style/AppTheme"
  • 在style.xml中定義不同風格的theme(對app而言的style啦)
  • 熟悉?attr/colorPrimary等屬性,在theme中定義好後,佈局文件用到這些屬性可以自動替換
  • 如何點擊觸發後整體畫面生效

代碼

1、清單文件manifest中,app默認使用AppTheme(或者自定義Theme)
<application
        ...
        android:theme="@style/AppTheme">
        ...
</application>
2、style.xml 風格:
  • 可定義app的theme,比方說AppTheme和MyTheme
  • 一般用來定義app內各種控件樣式,可重複使用,減少layout中的代碼量,亦可做到後期維護換色等快速修改,不用一一修改。
  • 用到一些自定義屬性
<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的人肯定熟悉):
  • 如果不用可忽略。這裏定義後,就可在style.xml中使用這個item屬性了,並可以寫?attr/myBgColor 和 ?attr/myButtonHeight
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="myBgColor" format="color" />
    <attr name="myButtonHeight" format="dimension" />
</resources>
4、layout.xml 佈局文件使用
  • 這裏背景使用到:android:background="?attr/myBgColor"
  • 其中一個按鈕用到:style="@style/MyBtnStyle",而這個style裏面屬性用到<item name="android:layout_height">?attr/myButtonHeight</item> 。 而且這個myButtonHeight定義到了2個theme裏面了!!!
<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、代碼控制切換主題
  • setTheme(),必須在setContentView(),繪畫畫面之前
  • 重啓當前畫面:recreate()
  • 當前畫面改了,但是除了棧頂的活動畫面,之後新打開畫面可以是新的theme,之前在棧裏存活的活動畫面還是不能及時換theme,這個此處不寫了,太多情況了,反正可以控制的。
//切換不同的風格,必須在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(); //重啓畫面
            }
        });
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章