我正在处理一些代码,我想在引用共享首选项时动态更改背景图像。活动我有一个例子是这样的:如何在Android中设置不同类的背景/布局
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)。
谢谢大家!
我更新了我的答案。它有帮助吗?或者我误解了你? – Suvitruf 2014-12-19 12:37:03