可以在开发过程为了让用户有更多的选择,会让用户自己选择背景图片(注意:不是主题哦!),我写了一个小例子share一下。
效果图:
原理很简单:就是SharedPreferences保存默认背景图片,所有需要设置layout都加载背景图片。
例子结构
源代码
ChangeBackgroundActivity.java
package com.harlan;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
/**
* 更换应用背景图片
* @author Harlan
* 2012-5-8
*/
public class ChangeBackgroundActivity extends Activity implements
OnClickListener {
Button btn1, btn2, btn3, btn4;
LinearLayout llayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn1 = (Button) findViewById(R.id.button1);
btn2 = (Button) findViewById(R.id.button2);
btn3 = (Button) findViewById(R.id.button3);
btn4 = (Button) findViewById(R.id.button4);
llayout = (LinearLayout) findViewById(R.id.llayout);
SetBackgroundImage.setBackGround(ChangeBackgroundActivity.this, llayout);
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
Intent intent = new Intent(ChangeBackgroundActivity.this,
Activity2.class);
startActivity(intent);
break;
case R.id.button2:
SetBackgroundImage.saveBackground(ChangeBackgroundActivity.this,
"bg1");
SetBackgroundImage.setBackGround(ChangeBackgroundActivity.this, llayout);
break;
case R.id.button3:
SetBackgroundImage.saveBackground(ChangeBackgroundActivity.this,
"bg2");
SetBackgroundImage.setBackGround(ChangeBackgroundActivity.this, llayout);
break;
case R.id.button4:
SetBackgroundImage.saveBackground(ChangeBackgroundActivity.this,
"bg3");
SetBackgroundImage.setBackGround(ChangeBackgroundActivity.this, llayout);
break;
}
}
}
SetBackgroundImage.java
package com.harlan;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.View;
/**
* 设置默认背景和更换背景
* @author Harlan
* 2012-5-7
*/
public class SetBackgroundImage {
public static void changeBackgroundImage(int i) {
}
/**
* 设置背景图片
* @param activity
* @param view 在这里传的是LinearLayout
*/
public static void setBackGround(Activity activity, View view) {
String bg = getBG(activity);
if (bg != null) {
if ("bg1".equals(bg)) {
view.setBackgroundResource(R.drawable.bg1);
}
if ("bg2".equals(bg)) {
view.setBackgroundResource(R.drawable.bg2);
}
if ("bg3".equals(bg)) {
view.setBackgroundResource(R.drawable.bg3);
}
}else{
saveBackground(activity,"bg1");
view.setBackgroundResource(R.drawable.bg3);
}
}
/**
* 更改默认背景图片
* @param activity
* @param imageTag
*/
public static void saveBackground(Activity activity, String imageTag) {
SharedPreferences preferences = activity.getSharedPreferences("bg",
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("background", imageTag);
editor.commit();
}
/**
* 得到默认图片标识
* @param activity
* @return
*/
private static String getBG(Activity activity) {
SharedPreferences preferences = activity.getSharedPreferences("bg",Activity.MODE_PRIVATE);
String bg = preferences.getString("background", null);
return bg;
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/llayout" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="跳转下一个Activity看效果" />
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="背景一" />
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="背景二" />
<Button
android:id="@+id/button4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="背景三" />
</LinearLayout>
o分源代码下载: http://download.csdn.net/detail/mark_dev/4284540
Over。
ps:偶只是按照我自己想的写的方法,如果有什么好的方法记得分享也学习一下。如果兴趣的可以加载我们的QQ群(213167832)