我正在處理一些代碼,我想在引用共享首選項時動態更改背景圖像。活動我有一個例子是這樣的:如何在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