歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android應用Theme

Android應用Theme

日期:2017/3/1 9:43:12   编辑:Linux編程

Android切換Theme主流三種方式來切換Theme,第一種是通過內置的style來切換,一般用於夜間模式/日間模式切換。第二種是通過apk來實現插件化,第三種是通過下載zip進行解壓到到相應的app文件下,應用講需要文件讀取到內存中。這篇是介紹第一種android切換Theme的方法。

Ubuntu 14.04 x64配置Android 4.4 kitkat編譯環境的方法 http://www.linuxidc.com/Linux/2014-05/101148.htm

Ubuntu 12.04搭建Android開發環境 http://www.linuxidc.com/Linux/2012-09/69961.htm

Ubuntu 14.04 配置 Android SDK 開發環境 http://www.linuxidc.com/Linux/2014-05/101039.htm

64位Ubuntu 11.10下Android開發環境的搭建(JDK+Eclipse+ADT+Android SDK詳細) http://www.linuxidc.com/Linux/2013-06/85303.htm

Ubuntu 12.10 x64 安裝 Android SDK http://www.linuxidc.com/Linux/2013-03/82005.htm

首先當然是在values下面創建attrs文件,然後定義了一些attr。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="main">
<attr name="bgColor" format="color"></attr>
<attr name="buttonBgColor" format="color"></attr>
<attr name="buttonTextColor" format="color"></attr>
<attr name="textSize" format="dimension"></attr>
</declare-styleable>
</resources>

然後再在styles文件定義兩個style。

01.<style name="dayTheme">
02. <item name="bgColor">#ffffff</item>
03. <item name="buttonBgColor">#80000000</item>
04. <item name="buttonTextColor">#80ffffff</item>
05. <item name="textSize">14sp</item>
06. </style>
07.
08. <style name="nightTheme">
09. <item name="bgColor">#cc000000</item>
10. <item name="buttonBgColor">#80ffffff</item>
11. <item name="buttonTextColor">#80000000</item>
12. <item name="textSize">14sp</item>
13. </style>

然後就是在布局文件中使用attrs。

<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"
android:background="?attr/bgColor"
>

<Button
android:id="@+id/swtichThemeBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="?attr/textSize"
android:text="按下按鈕切換夜間模式"
android:layout_margin="10dip"
android:background="?attr/buttonBgColor"
android:textColor="?attr/buttonTextColor"
/>

</LinearLayout>

最後就是在mainActivity設置theme並動態切換theme。

import android.os.Bundle;
import android.preference.PreferenceManager;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

private Button mSwtichThemeBtn;
private boolean isNight;
private SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sp = PreferenceManager.getDefaultSharedPreferences(this);
setTheme((isNight = sp.getBoolean("isNight", false)) ? R.style.nightTheme : R.style.dayTheme);
setContentView(R.layout.activity_main);
mSwtichThemeBtn = (Button) this.findViewById(R.id.swtichThemeBtn);
mSwtichThemeBtn.setText(isNight?"切換日間模式":"切換夜間模式");
mSwtichThemeBtn.setOnClickListener(new View.OnClickListener() {


@Override
public void onClick(View v) {
Editor edit = sp.edit();
edit.putBoolean("isNight", !isNight);
edit.commit();
recreateForTheme();
}
});
}
@SuppressLint("NewApi")
public void recreateForTheme(){

if(android.os.Build.VERSION.SDK_INT >= 11){
this.recreate();
}else{
this.finish();
startActivity(new Intent(MainActivity.this,MainActivity.class));
}
}

更多詳情見請繼續閱讀下一頁的精彩內容: http://www.linuxidc.com/Linux/2014-06/103066p2.htm

Copyright © Linux教程網 All Rights Reserved