夜间模式1

备注:把清单配置文件中的主题名字改掉

主界面:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utils.zhuti(this);
        setContentView(R.layout.activity_main);
    }
    public void Button(View view){
        Utils.gaibian(this);
    }
} 
Utils类:

public class Utils {
    public static int theme = 0;
    public static final int DAY_THEME = 0;
    public static final int NIGHT_THEME = 1;
    public static void zhuti(Activity activity){
        switch (theme){
            case DAY_THEME:
                activity.setTheme(R.style.day_Theme);
                break;
            case NIGHT_THEME:
                activity.setTheme(R.style.night_Theme);
                break;
        }
    }
    public static void gaibian(Activity activity){
        switch (theme){
            case DAY_THEME:
                theme = NIGHT_THEME;
                break;
            case NIGHT_THEME:
                theme = DAY_THEME;
                break;
        }
        activity.finish();
        activity.overridePendingTransition(R.anim.in,R.anim.out);
        activity.startActivity(new Intent(activity,activity.getClass()));
    }
}
attrs类:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="wenziyanse" format="color"></attr>
    <attr name="wenzineirong" format="string"></attr>
</resources>
Color类:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="background">#252a2e</color>
    <color name="unablebtn">#dcdcdc</color>
    <color name="dark_bg">#505050</color>
    <color name="light">#ECECEC</color>
    <color name="white">#FFFFFF</color>
    <color name="black">#000000</color>
    <color name="green">#05D992</color>
    <color name="zise">#E5004F</color>
    <color name="dark_bg1">#414141</color>
    <color name="pink">#FF5877</color>
    <color name="yellow">#FFFF00</color>
</resources>
String类:

<resources>
    <string name="app_name">YeJianMoShi</string>
    <string name="night">切换成夜间模式</string>
    <string name="day">切换成日间模式</string>
</resources>
style类:

<resources>
    <!-- Base application theme. -->
    <style name="day_Theme" 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>
        <item name="wenziyanse">@color/black</item>
        <item name="wenzineirong">@string/night</item>
        <item name="android:windowBackground">@color/white</item>
    </style>
    <style name="night_Theme" 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>
        <item name="wenziyanse">@color/white</item>
        <item name="wenzineirong">@string/day</item>
        <item name="android:windowBackground">@color/dark_bg</item>
    </style>
</resources>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值