Java 如何在Android中设置来自不同类的类的背景/布局
我正在使用一些代码,希望在引用共享首选项时动态更改背景图像。我的一个活动示例如下:Java 如何在Android中设置来自不同类的类的背景/布局,java,android,layout,sharedpreferences,setbackground,Java,Android,Layout,Sharedpreferences,Setbackground,我正在使用一些代码,希望在引用共享首选项时动态更改背景图像。我的一个活动示例如下: public class Splash extends Activity { protected void onCreate(Bundle inputVariableToSendToSuperClass) { super.onCreate(inputVariableToSendToSuperClass); setContentView(R.layout.splash);
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活动中的布局(setBackgroundDrawable)
谢谢大家 1)你做错了。您不应该使用new
直接创建活动
2) 您应该使用Intent
打开新活动,并为其设置参数
Intent intent = new Intent(context, ManagePreferences.class);
intent.putExtra("user_choice", user_choice);
startActivity(intent);
在ManagePreferences
中获取:
Bundle extras = getIntent().getExtras();
if (extras != null) {
String user_choice = extras.getString("user_choice");
}
UPD:如果您像实用程序类一样使用ManagePreferences
,请将setTheBackground
static:
public static 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
}
layout.requestLayout();
}
并称之为:
ManagePreferences.setTheBackground(this, user_choice, layout);
UPD:正如所回答的,您不能这样做。当您使用findViewById()
引用布局文件时,android系统仅在当前的ContentView
中查找该布局文件。(即您为当前活动使用setContentView()
设置的视图)。关于通过意图传递数据,您肯定是正确的,问题是,我没有打开其他类。ManagePreferences活动从未作为类打开。我只是想使用它,所以我必须运行10条if语句来设置背景图像。我正在试图找到一种方法,使layout.setBackgroundDrawable()引用最初调用该方法的类。基本上,让ManagePreferences类为调用的类实际设置布局it@Silmarilos我真的不明白为什么要使用Activity而从不打开它=/i更新了这个问题以表明ManagePreferences类不是Activity。这有助于让事情变得更清楚一点吗?我不是试图打开一个活动,而是试图使用ManagePreferences类来更改splash的布局class@Silmarilos嗯,试着将背景设置为静态。您将调用它,而不创建类的实例。设置bg后,尝试在layoutTried上调用requestLayout(),但运气不佳:(显然,这是无法做到的。请查看此处的第二个答案:。我非常感谢您帮助尝试Suvitruf!您是否希望发布一个链接,并提及我们无法更改答案中的UI组件,我将选择它作为正确答案?再次感谢!我更新了我的答案。它有帮助吗?还是我误解了U