android 樣式 與 主題的動態設置

首先看效果圖



第一步:


代碼:

 <!-- 自定義樣式 -->
    <style name="text_content_style">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textColor">#ff0000</item>
        <item name="android:textSize">30sp</item>
    </style>
    <!-- 樣式繼承 -->

    <style name="day_text" parent="@style/text_content_style">
        <item name="android:background">#00ff00</item>
        <item name="android:textColor">#0000ff</item>
    </style>
    <!-- 繼承了系統主題 -->
    <style name="day_theme" parent="@android:style/Theme.NoTitleBar">
        <item name="android:windowBackground">@drawable/effect_env_auditorium</item>
    </style>

    <style name="night_theme">
        <item name="android:background">#000000</item>
    </style>

第二步:

代碼中:

package com.example.teststyletheme;

import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnClickListener {

	Button dayBtn, nightBtn;

	private int THEMEID = -1;// 設置主題id

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		if (savedInstanceState != null) {
			if (savedInstanceState.getInt("theme", -1) != -1) {
				THEMEID = savedInstanceState.getInt("theme");
				this.setTheme(THEMEID); // 設置主題皮膚
			}
		}
		setContentView(R.layout.activity_main);

		dayBtn = (Button) findViewById(R.id.day);
		nightBtn = (Button) findViewById(R.id.night);

		dayBtn.setOnClickListener(this);
		nightBtn.setOnClickListener(this);

	}

	// 設置主題,並重建
	private void onTheme(int iThemeId) {
		THEMEID = iThemeId;
		this.recreate();
	}

	// 保存主題ID,onCreate 時讀取主題
	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt("theme", THEMEID);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.day:
			onTheme(R.style.day_theme);
//			onTheme(android.R.style.Theme_Holo_Light);
			break;

		case R.id.night:
			onTheme(R.style.night_theme);
			break;
		}

	}

}

附上佈局代碼:

<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:orientation="vertical" >

    <TextView
        style="@style/text_content_style"
        android:text="我使用了樣式" />

    <TextView
        style="@style/day_text"
        android:text="我使用了繼承樣式" />

    <Button
        android:id="@+id/day"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="圖片主題day" />

    <Button
        android:id="@+id/night"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="主題night" />

</LinearLayout>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章