这是其中一种实现模式,也是比较麻烦的一种,首先写布局,不多说上代码
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context="com.example.day_night_demo2.MainActivity"> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="onClick" android:text="?attr/textContent"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="100dp" android:text="Hello World!" android:textColor="?attr/textColorValue" /> <TextView android:textColor="?attr/textColorValue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dp" android:text=