android 動態切換主題,動態換膚

來源:互聯網
上載者:User

android 動態換膚,Android系統已經完美解決換膚問題,看到網上好多的朋友還在詢問如何換膚。我樣本一下Android換膚

看效果

點擊 Day 後的主題皮膚

點擊 Night後,主題皮膚

package cn.hpc.assistant;import android.app.Activity;import android.content.Context;import android.graphics.PixelFormat;import android.graphics.Point;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.Gravity;import android.view.View;import android.view.WindowManager;import android.view.WindowManager.LayoutParams;public class MainActivity extends Activity {private int mThemeId = -1; // 皮膚主題ID,預設-1 不處理@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);if (savedInstanceState != null) {if (savedInstanceState.getInt("theme", -1) != -1) {// 讀取皮膚主題ID,-1 不處理mThemeId = savedInstanceState.getInt("theme");this.setTheme(mThemeId);  //設定主題皮膚}}setContentView(R.layout.activity_main);this.findViewById(R.id.id_btn_day).setOnClickListener(mOnClickListener);   // day 明亮主題this.findViewById(R.id.id_btn_night).setOnClickListener(mOnClickListener);  //night 黑暗主題}View.OnClickListener mOnClickListener = new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.id_btn_day:onTheme(android.R.style.Theme_Light);break;case R.id.id_btn_night:onTheme(android.R.style.Theme_Black);break;default:}}};          // 設定主題,並重建private void onTheme(int iThemeId){mThemeId = iThemeId;this.recreate();}// 儲存主題ID,onCreate 時讀取主題@Overridepublic void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);outState.putInt("theme", mThemeId);}}

布局檔案:

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="horizontal"    tools:context=".MainActivity" >    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:text="@string/hello_world" />    <Button        android:id="@+id/id_btn_fun"        android:layout_width="wrap_content"        android:layout_height="wrap_content" />    <Button        android:id="@+id/id_btn_day"        android:layout_width="wrap_content"        android:layout_height="wrap_content"         android:text="Day"/>    <Button        android:id="@+id/id_btn_night"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="Night" /></LinearLayout>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.