动态改变Android Theme
mob 发布于 2023-01-27

1)添加开始和结束标签

<resources>

</resources>

2)为主题指定一个唯一的名称,并添加结束“style”标记:

<resources>

<style name="BlackTheme" >

</style>

</resources>

3)使用成对的XML属性和值定义主题的视觉方面:

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

在本教程中,我们将创建定义不同背景颜色和文本颜色的两个主题:

<resources>

<style name="BlackTheme" >

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

<item name="android:textColor">#FFFFFF</item>

</style>

<style name="BlueTheme" >

<item name="android:background">#B0E0E6</item>

<item name="android:textColor">#000000</item>

</style>

</resources>

 

创建Layout

为了检查主题切换功能是否正常,我们将创建一个突出显示文本和背景颜色变化的布局。我们还将为用户提供一种在主题之间切换的方法,当然,首先要让他们知道应用程序有这个选项!

打开布局文件并输入以下内容:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns_android="http://schemas.android.com/apk/res/android"

android_layout_width="fill_parent"

android_layout_height="fill_parent"

android_orientation="vertical" >

<TextView

android_id="@+id/textView1"

android_layout_width="wrap_content"

android_layout_height="wrap_content"

android_layout_alignParentTop="true"

android_layout_centerHorizontal="true"

android_layout_marginTop="14dp"

android_text="@string/pick"

android_textAppearance="?android:attr/textAppearanceLarge" />

<Button

android_id="@+id/blackbutton"

android_layout_width="fill_parent"

android_layout_height="wrap_content"

android_layout_alignParentLeft="true"

android_layout_below="@+id/textView1"

android_text="@string/black" />

<Button

android_id="@+id/bluebutton"

android_layout_width="fill_parent"

android_layout_height="wrap_content"

android_layout_alignRight="@+id/blackbutton"

android_layout_below="@+id/blackbutton"

android_text="@string/blue" />

<EditText

android_id="@+id/editText1"

android_layout_width="wrap_content"

android_layout_height="wrap_content"

android_layout_alignLeft="@+id/textView2"

android_layout_below="@+id/bluebutton"

android_ems="10"

android_hint="Name"

android_inputType="textPersonName" >

</EditText>

<TextView

android_id="@+id/textView2"

android_layout_width="wrap_content"

android_layout_height="wrap_content"

android_layout_alignRight="@+id/editText3"

android_layout_below="@+id/editText3"

android_text="@string/agree" />

<EditText

android_id="@+id/editText3"

android_layout_width="wrap_content"

android_layout_height="wrap_content"

android_layout_alignParentLeft="true"

android_layout_below="@+id/editText1"

android_ems="10"

android_hint="Password"

android_inputType="numberPassword" />

</RelativeLayout>

定义Strings

正如您可能已经注意到的,布局引用了几个字符串。打开res/values/strings.xml并添加以下内容:

<string name="black">Black</string>
<string name="blue">Blue</string>
<string name="pick">Pick Your Colour</string>
<string name="agree">I agree to the terms and conditions</string>

 

MainActivity.java

现在困难来了:实现实际功能!打开src/package目录中的MainActivity.java文件。

我们将分几节阅读代码,并强调一些最重要的要点。

import android.os.Bundle;

import android.app.Activity;

import android.view.View;

import android.view.View.OnClickListener;

这些是应用程序中使用的各种类和接口的导入语句。“OnClickListener”特别重要,因为它允许在单击按钮时调用回调。

{

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) 

{

super.onCreate(savedInstanceState);

themeUtils.onActivityCreateSetTheme(this);

setContentView(R.layout.activity_main);

这里,您的“Activity”正在实现最重要的OnClickListener。现在,忽略Eclipse对themeUtils.onActivityCreateSetTheme抛出的错误

findViewById(R.id.blackbutton).setOnClickListener(this);

findViewById(R.id.bluebutton).setOnClickListener(this);

}

告诉OnClickListener它应该“监听”哪些视图。

@Override

public void onClick(View v) 

{

switch (v.getId())

{


case R.id.blackbutton:

themeUtils.changeToTheme(this, themeUtils.BLACK);

break;

case R.id.bluebutton:

themeUtils.changeToTheme(this, themeUtils.BLUE);

break;

}

}

}

主题不可能同时为黑色和蓝色,因此break语句(如果按下的按钮为黑色,则程序无需运行蓝色按钮代码)

注意,最后一节将抛出大量错误,但在下一步中,我们将创建themeUtils类,这些警告将消失。

创建themeUtils.java

下一步是创建我们在MainActivity.java中引用的新类,并创建一些静态方法来支持我们的主题更改功能。要创建新类,请打开src/package目录。右键单击,打开“新建”菜单并选择“类”选项。输入名称“themeUtils”

打开新文件并输入以下内容:

package com.jessica.theme;



import android.app.Activity;

import android.content.Intent;


 public class themeUtils

{

private static int cTheme;



public final static int BLACK = 0;

public final static int BLUE = 1;

public static void changeToTheme(Activity activity, int theme)

{

cTheme = theme;

activity.finish();



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


 }

public static void onActivityCreateSetTheme(Activity activity)

{

switch (cTheme)

{

default:

case BLACK:

activity.setTheme(R.style.BlackTheme);

break;

case BLUE:

activity.setTheme(R.style.BlueTheme);

break;

}

}

}

参考文章:Changing Your Android App’s Theme Dynamically

mob
关注 私信
文章
61
关注
0
粉丝
0