創建一個定義不同的背景顏色和文字顏色兩個主題:
<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>
打開您的佈局文件,輸入如下內容:
<?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.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>
打開新的文件,並輸入以下內容:
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;
}
}
}
MainActivity.java
public class MainActivity extends Activity implements 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);
findViewById(R.id.blackbutton).setOnClickListener(this);
findViewById(R.id.bluebutton).setOnClickListener(this);
}
@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;
}
}
}
看看你的工作。
你想整個應用程序的主題改變? –
是的......你說得對 –