java - 如何在 Android 中设置来自不同类的类的背景/布局

标签 java android layout sharedpreferences setbackground

我正在使用一些代码,我想在引用共享首选项的同时动态更改背景图像。我的 Activity 示例如下:

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 Activity 中,我可以使代码正常工作,但如果我引用该类并调用该方法则不行;我不想让我的代码变得困惑。

我想做的就是通过调用此 ManagePreferences 类来更改 Splash Activity 中的布局 (setBackgroundDrawable)。

谢谢大家!

最佳答案

1)你做错了。您不应使用 new 直接创建 Activity

2) 您应该使用 Intent 打开新的 Activity 并为其设置参数。

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设为静态:

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:已答复here , 你不可以做这个。当您使用 findViewById() 引用布局文件时,Android 系统仅在当前的 ContentView 中查找该布局文件。 (即您使用 setContentView() 为当前 Activity 设置的 View )。

关于java - 如何在 Android 中设置来自不同类的类的背景/布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27562445/

相关文章:

java - 如何将 OnClickListener 设置为 Expandable RecycleView

android - 如何在android中的textview上获取一个值作为htmltext?

java - 对象实例重用怎么样?

html - 全宽显示子 div

java - 如何使用 JSoup 从 Android 中的 url 中的嵌入式 url 获取远程视频的直接链接?

java - 快速查询包含键的表(DynamoDB 和 Java)

java - 当textview是listview的一部分时,如何使用SimpleAdapter更改textview的字体

java - 调用 GetText() 时出现 NullPointerException

java - 如何允许用户调整小部件的大小?

java - 在 Java 中 - 如何将 resultSet 映射到复杂对象?