软件换肤从功能上可以划分三种:
就像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{
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("单击按钮");
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;
public static void changeToTheme(Activity activity, int theme)
{
sTheme = theme;
activity.finish();
activity.startActivity(new Intent(activity, activity.getClass()));
}
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>
<img src="http://my.csdn.net/uploads/201205/13/1336912506_5495.png" alt="">