Android动态切换主题

简介: <div style="font-family:微软雅黑; font-size:18px"><span style="color:rgb(51,51,51); font-family:Arial; font-size:15px; line-height:26px; text-align:left">软件换肤从功能上可以划分三种:</span></div> <div style="font
软件换肤从功能上可以划分三种:

1) 软件内置多个皮肤,不可由用户增加或修改;

最低的自由度,软件实现相对于后两种最容易。

2) 官方提供皮肤供下载,用户可以使用下载的皮肤;

用户可选择下载自己喜欢的皮肤,有些玩家会破解皮肤的定制方法,自己做皮肤使用,或者传到网上给大家用。

参考:http://blog.csdn.net/zhyooo123/article/details/6697186

3) 官方提供皮肤制作工具或方法,用户可自制皮肤。



关于主题和样式:

就像style一样,主题依然在<style>元素里边申明,也是以同样的方式引用。
不同的是你通过在Android Manifest中定义的<application>和<activity>元素将主题添加到整个程序或者某个 Activity,但是主题是不能应用在某一个单独的View里。

@符号和?符号来应用资源。@符号表明了我们应用的资源是前边定义过的(或者在前一个项目中或者在Android 框架中)。问号?表明了我们引用的资源的值在当前的主题当中定义过

关于设置主题的注意事项:
不少同学会发泄setTheme()竟然会无效。那么注意
使用setTheme()只能在Oncreate()之前使用。在setContentView(),还是不行那么就在super.onCreate(savedInstanceState);之前
如果要使用动态切换主题,那么就必须调用actvity.finish()。然后再重新加载setTheme()

一些参考资料:








接下来来看范例:

设有一个main.xml布局文件。
新建一个xml用于放置多个主题。如:

<--蓝色主题-->
<style name="Theme.Blue">
<item name=" pageBackground">@style/page_background_bl</item>
<item name="pagePaddingLayout">@style/page_padding_layout_bl</item>
</style>

<--白色主题-->
<style name="Theme.White">
<item name=" pageBackground">@style/page_background_wh</item>
<item name="pagePaddingLayout">@style/page_padding_layout_wh</item> </style>

注意到这里每个主题中的item名字是相同的,且在布局文件main.xml中
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
style="? pageBackground">
main.xml中引用白色主题还是蓝色主题的pageBackground,交由代码处理。动态切换主题。

代码实现动态切换:
创建一个util类,设置一个全局变量保存主题信息。
那么就必须调用actvity.finish()。然后再重新加载setTheme()

下面贴出主要的代码:
package irdc.ex03_21; 


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


public class EX03_21 extends Activity implements OnClickListener{ 
  /** Called when the activity is first created. */ 
  Button button = null;
  @Override 
  public void onCreate(Bundle savedInstanceState) {
    
    Utils.onActivityCreateSetTheme(this);
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    
    findViewById(R.id.button1).setOnClickListener(this);
    findViewById(R.id.button2).setOnClickListener(this);
    findViewById(R.id.button3).setOnClickListener(this);
    
  }
  @Override
  public void onClick(View v)
  {
    System.out.println("单击按钮");
    // TODO Auto-generated method stub
    switch (v.getId())
    {
    case R.id.button1:
      System.out.println("主题1");
      Utils.changeToTheme(this, 1);
      break;
    case R.id.button2:
      System.out.println("主题2");
      Utils.changeToTheme(this, 2);
      break;
    case R.id.button3:
      System.out.println("主题3");
      Utils.changeToTheme(this, 3);
      break;
    }
    
  }
}

package irdc.ex03_21;

import android.app.Activity;
import android.content.Intent;

public class Utils
{
	private static int sTheme;

	public final static int THEME_DEFAULT = 0;
	public final static int THEME_WHITE = 1;
	public final static int THEME_BLUE = 2;

	/**
	 * Set the theme of the Activity, and restart it by creating a new Activity
	 * of the same type.
	 */
	public static void changeToTheme(Activity activity, int theme)
	{
		sTheme = theme;
		activity.finish();

		activity.startActivity(new Intent(activity, activity.getClass()));
	}

	/** Set the theme of the activity, according to the configuration. */
	public static void onActivityCreateSetTheme(Activity activity)
	{
		switch (sTheme)
		{
		default:
		case 1:
		  activity.setTheme(R.style.Theme_Translucent);
			break;
		case 2:
			activity.setTheme(R.style.Theme_Translucent2);
			break;
		case 3:
			activity.setTheme(R.style.Theme_Transparent);
			break;
		}
	}
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <TextView
  android:textColor="@drawable/darkgreen"
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="@string/str_text_view1"
  />

  <Button
      android:id="@+id/button2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="主题1" />

  <Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="主题2" />

  <Button
      android:id="@+id/button3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="主题3" />

</LinearLayout>

 
 

目录
相关文章
|
3月前
|
Android开发 开发者
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
Android UI设计: 请解释Activity的Theme是什么,如何更改应用程序的主题?
42 1
|
1天前
|
Android开发
Android RIL 动态切换 4G 模块适配
Android RIL 动态切换 4G 模块适配
4 0
|
1天前
|
Android开发
Android 动态修改参数配置
Android 动态修改参数配置
8 0
|
9天前
|
安全 Android开发 数据安全/隐私保护
Android中的动态权限请求与最佳实践
【4月更文挑战第14天】 在现代安卓应用开发中,用户隐私和安全被赋予了前所未有的重要性。随着Android 6.0(API级别23)引入的运行时权限模型,开发者必须更加细致地处理权限请求,以确保应用功能的完整性同时不侵犯用户的隐私。本文将深入探讨如何在Android应用中实现动态权限请求,分析常见问题,并提供一系列最佳实践,以帮助开发者优雅地处理这一挑战。
19 5
|
4月前
|
XML Java Android开发
Android App开发之图像加工中给图像添加水波动态特效(附源码和演示视频 简单易懂)
Android App开发之图像加工中给图像添加水波动态特效(附源码和演示视频 简单易懂)
40 0
|
9月前
|
Android开发 iOS开发 Windows
无影产品动态|iOS & Android客户端6.0.0版本发布,提升触控灵敏度,操作体验更丝滑
无影ios & Android客户端6.0.0版本发布!移动端触控体验更舒适,用户操作更便捷,一起来看看!
676 0
无影产品动态|iOS & Android客户端6.0.0版本发布,提升触控灵敏度,操作体验更丝滑
|
9月前
|
双11 Android开发
Android动态来改变App桌面图标
其实对于这样的一个桌面图标更换,Android中为我们提供了AndroidManifest.xml里的<activity-alias>标签实现方式。
222 0
|
10月前
|
安全 Android开发
Android 动态权限 permission 操作类简单封装
Android 动态权限 permission 操作类简单封装
|
11月前
|
XML 编解码 Android开发
Android 设置Padding和Margin(动态/静态)
在Android界面开发时,为了布局更加合理好看,很多时候会用上Padding和Margin, padding和margin是什么呢?即内边距和外边距; 某个View指定为padding是针对该View里面的子View距离该View距离而言的,或者是里面的内容距离容器的距离。