gpt4 book ai didi

java - 在 Activity 启动时更改线性布局背景图像

转载 作者:行者123 更新时间:2023-11-29 06:42:37 24 4
gpt4 key购买 nike

我通过此代码更改线性布局背景图片:

mainlayout.setBackgroundResource(R.drawable.back);

但我想在每次 Activity 开始时执行此操作,在第一次启动和 Activity 之间切换时。我将这段代码放在 onCreate() 的 IF 语句中,但背景没有改变!当然,当我将此代码设置为按钮时效果很好!我必须如何以及在何处放置我的代码?

我的完整代码是:

            //check theme id
if(myDbHelper.gettheme()==1)
{
mainlayout.setBackgroundResource(R.drawable.back);
}else if(myDbHelper.gettheme()==2)
{
mainlayout.setBackgroundResource(R.drawable.blueback);
}

最佳答案

您需要将代码放入 onResume() 方法中。

以下是解释原因的详细信息 http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

关于java - 在 Activity 启动时更改线性布局背景图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9727461/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com