Android切换白黑主题,通过attr更新控件样式

  • Post author:
  • Post category:其他



话不多说,直接上代码



一,在res/values文件下新建一个attrs文件

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="custom_text_bg_color" format="color" />
    <attr name="custom_text_color" format="color" />
    <attr name="custom_icon" format="reference" />
    <attr name="custom_wireframe" format="reference" />
</resources>


二,在styles中新建黑白主题

<resources>
    <!-- Base application theme. -->
    <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="Night" parent="Theme.AppCompat">
        <item name="custom_text_bg_color">#FF171717</item>
        <item name="custom_text_color">#FFEDEFF2</item>
        <item name="custom_icon">@drawable/icon_facebook</item>
        <item name="custom_wireframe">@drawable/border_round_gray_classic</item>
    </style>

    <!--日间主题-->
    <style name="Daytime" parent="Theme.AppCompat">
        <item name="custom_text_bg_color">#FFEDEFF2</item>
        <item name="custom_text_color">#D81B60</item>
        <item name="custom_icon">@drawable/icon_twitter</item>
        <item name="custom_wireframe">@drawable/border_round_comparison_dark</item>
    </style>
</resources>


三,给activity贴上布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:id="@+id/tv_click"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="?attr/custom_text_bg_color"
        android:gravity="center"
        android:text="Hello World!"
        android:textColor="?attr/custom_text_color" />

    <ImageView
        android:id="@+id/iv_icon"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_gravity="center"
        android:layout_margin="20dp" />

    <EditText
        android:id="@+id/ed_wireframe"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:layout_margin="20dp"
        android:text="线框示例"
        android:background="?attr/custom_wireframe" />

    <Button
        android:id="@+id/but_Theme"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="切换主题" />
</LinearLayout>


四,上居然实现代码

public class MainActivity extends AppCompatActivity {
    boolean themeFlag;
    private int textColorBg, textColor;
    private Drawable iconResources;
    private TextView textView;
    private ImageView icon;
    private Button butTheme;
    SharedPreferences sharedPreferences;
    SharedPreferences.Editor editor;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        sharedPreferences = this.getSharedPreferences("spTheme", MODE_PRIVATE);
        editor = sharedPreferences.edit();
        themeFlag = sharedPreferences.getBoolean("theme", true);

        if (themeFlag) {
            setTheme(R.style.Daytime);
        } else {
            setTheme(R.style.Night);
        }
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.tv_click);
        icon = findViewById(R.id.iv_icon);
        butTheme = findViewById(R.id.but_Theme);

        TypedArray ta = obtainStyledAttributes(new int[]{
                R.attr.custom_text_bg_color,
                R.attr.custom_text_color,
                R.attr.custom_icon,//icon图标
                R.attr.custom_wireframe,//线框
        });

        //int up_normal = ta.getDrawable(0);
        textColorBg = ta.getColor(0, -1);
        textColor = ta.getColor(1, -1);
        iconResources = ta.getDrawable(2);
        textView.setText(themeFlag?"日间模式":"夜间模式");
        //设置window背景
        getWindow().setBackgroundDrawableResource(themeFlag?
                R.color.custom_attr_btn_bg_color_daytime:R.color.custom_attr_btn_bg_color_night);
        Log.e("textColor ", textColor + "");
        Log.e("textColorBg ", textColorBg + "");

        ta.recycle();
        //textView.setTextColor(textColor); //也可在xml控件中实现
        //textView.setBackgroundColor(textColorBg);
        icon.setImageDrawable(iconResources);

        butTheme.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (themeFlag) {//日间模式
                    editor.putBoolean("theme", false).commit();//记录修改后的主题
                    setTheme(R.style.Daytime);
                } else {
                    editor.putBoolean("theme", true).commit();
                    setTheme(R.style.Night);
                }

                Intent mIntent = getIntent();
                finish();
                overridePendingTransition(0, 0);//设置activity无痕切换动画
                startActivity(mIntent);
            }
        });
    }
}


五,附线框drawable资源


①classic

<?xml version="1.0" encoding="UTF-8"?>
	<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">

	<corners android:radius="3dp"/>

	<solid android:color="#FFFFFF"/>

	<padding android:bottom="3dp"  android:top="3dp" />

	<stroke android:color="#BEBEBE" android:width="1dp"/>
</shape>


②dark

<?xml version="1.0" encoding="UTF-8"?>
	<shape android:shape="rectangle" xmlns:android="http://schemas.android.com/apk/res/android">

	<corners android:radius="3dp"/>

	<solid android:color="#0F1C24"/>

	<padding android:bottom="3dp"  android:top="3dp" />

	<stroke android:color="#0F1C24" android:width="1dp"/>
</shape>


六,看看效果图


在这里插入图片描述



版权声明:本文为sunshine_mood原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。