我正在使用一些代码,我想在引用共享首选项的同时动态更改背景图像。我有一个活动的例子是这样的:
public class Splash extends Activity {
protected void onCreate(Bundle inputVariableToSendToSuperClass) {
super.onCreate(inputVariableToSendToSuperClass);
setContentView(R.layout.splash);
Initialize();
//Setting background
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String user_choice = prefs.getString("pref_background_choice","blue_glass");
LinearLayout layout = (LinearLayout) findViewById(R.id.activity_splash_layout);
ManagePreferences mp = new ManagePreferences();
mp.setTheBackground(Splash.this, user_choice, layout);
//More code after this...
}
}
ManagePreferences 类如下所示:
public class ManagePreferences {
//Empty Constructor
public ManagePreferences(){
}
public void setTheBackground(Context context, String background_choice, LinearLayout layout){
if (background_choice == "blue_glass"){
layout.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.blue_glass));
} else if (background_choice == "blue_oil_painting")
//etc... with more backgrounds
}
}
问题是,设置背景的代码在不同的类中不起作用。如果我将代码复制到 Splash 活动中,我可以让代码工作,但如果我引用类并调用方法,则不能;我宁愿不要弄乱我的代码。
我要做的就是通过调用这个 ManagePreferences 类来更改 Splash Activity 中的布局(setBackgroundDrawable)。
谢谢大家!