日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

android简单的夜间模式

發布時間:2023/12/20 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android简单的夜间模式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

現在android項目values下打

attrs.xml

<?xml version="1.0" encoding="utf-8"?> <resources><attr name="bookimage" format="reference|color" /><attr name="tvcolor" format="reference|color" /> </resources> ?
??

style.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 默認風格 -->
<style name="BrowserThemeDefault" parent="@android:style/Theme.Black.NoTitleBar">
<item name="bookimage">@android:color/white</item>
<item name="tvcolor">@android:color/darker_gray</item>
</style>

<!-- 夜間模式 -->
<style name="BrowserThemeNight" parent="@android:style/Theme.Black.NoTitleBar">
<item name="bookimage">@android:color/transparent</item>
<item name="tvcolor">@android:color/white</item>
</style>
</resources>

?
??

layout下activity_main

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"
android:layout_height="match_parent"
//界面顏色改變
android:background="?bookimage"
>

?

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"
//字體顏色改變
android:textColor="?tvcolor"/>

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
//監聽方法

android:onClick="btonclick"
android:text="日/夜間模式切換" />

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="116dp"
android:onClick="btonclick2"
android:text="跳轉其他頁面" />

</RelativeLayout>

?
??

MainActivity

package com.example.zdndemo;

?

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

public class MainActivity extends Activity {
private static boolean blFlag = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);



SharedPreferences preferences = getSharedPreferences("default_night",
MODE_PRIVATE);
blFlag = preferences.getBoolean("default_night",true);
if (blFlag) {
this.setTheme(R.style.BrowserThemeDefault);
}
else {
this.setTheme(R.style.BrowserThemeNight);
}
//上面的代碼必須要放在setContentView之上

setContentView(R.layout.activity_main);
}

public void btonclick(View view) {
SharedPreferences preferences = getSharedPreferences("default_night",MODE_PRIVATE);
Editor editor = preferences.edit();
if (blFlag) {
this.setTheme(R.style.BrowserThemeNight);
blFlag =false;
editor.putBoolean("default_night",false);
} else {
this.setTheme(R.style.BrowserThemeDefault);
blFlag = true;
editor.putBoolean("default_night",true);

}
// 提交修改
editor.commit();
this.setContentView(R.layout.activity_main);
//不行的話在跳下本頁面

}

public void btonclick2(View view) {
Intent intent = new Intent();
intent.setClass(this, breakactivity.class);
startActivity(intent);
}
}

?
??

轉載于:https://www.cnblogs.com/nanze/p/5384137.html

總結

以上是生活随笔為你收集整理的android简单的夜间模式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。