您的位置:首页 > 移动开发 > Android开发

Android动态切换主题

2012-05-13 20:32 141 查看

Android动态切换主题

分类:
android 2012-05-13 20:32
1066人阅读 评论(1)
收藏
举报

软件换肤从功能上可以划分三种:

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()

一些参考资料:
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=160087
http://www.eoeandroid.com/forum.php?mod=viewthread&tid=103123
android自带主题和样式

接下来来看范例:

设有一个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()

下面贴出主要的代码:

[java]
view plaincopyprint?

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.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;
}

}
}


[java]
view plaincopyprint?

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;  
        }  
    }  
}  

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;
}
}
}


[html]
view plaincopyprint?

<?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>  

<?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>


[java]
view plaincopyprint?

<IMG alt="" src="http://my.csdn.net/uploads/201205/13/1336912506_5495.png">  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: