以编程方式设置活动背景时,背景变成黑色

5

我有一个全屏的活动,想要以编程方式设置背景。 我在我的drawable文件夹中有四个不同的图像,每次创建活动时,我都想随机选择其中一个作为背景。以下是我的代码:

LayoutInflater inflater = getLayoutInflater();
FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.activity_my, null);
int[] images = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4};
Random rand = new Random();
layout.setBackgroundResource(images[rand.nextInt(images.length)]);

这是XML文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.mkessler.MyApp.MyActivity">


<TextView
    android:id="@+id/fullscreen_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:keepScreenOn="true"
    android:textColor="#33b5e5"
    android:textSize="50sp"
    android:textStyle="bold" />


<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <LinearLayout
        android:id="@+id/fullscreen_content_controls"
        style="?metaButtonBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:background="@color/black_overlay"
        android:orientation="horizontal"
        tools:ignore="UselessParent">

        <Button
            android:id="@+id/dummy_button"
            style="?metaButtonBarButtonStyle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Unimportant text"
            android:onClick="someFunction"/>

    </LinearLayout>
</FrameLayout>

然而,当我在手机上运行应用程序时,背景只是黑色的。 当我尝试使用特定的图像进行设置时,同样的情况也发生了。 不过,当我从XML文件中设置背景时,一切都很正常。
编辑: 仅为澄清起见,即使我尝试通过编程方式设置给定的图像而非XML文件,我仍然会得到黑色背景。 我认为将问题随意解释不会有任何进展。
编辑#2:我的项目的最低API设置为15。 不知道这是否相关,但以防有人认为它很重要...

你的随机数可能大于图像数组的大小,请确保它在0和3之间。 - Asmaa Rashad
@AsmaaRashad 难道不是 rand.nextInt(images.length) 给我的吗? - faerubin
Logcat也会显示警告--只需监视它,看看当您更改图像时是否显示任何可疑内容。 - Tasos
@Tasos,复制到评论中太多了,但据我所知,logcat中没有关于图像的任何信息。 - faerubin
在AS中--为了仅过滤应用程序,请在搜索框中添加您的包名称,例如(com.nyapplicaionname)--您将获得较少的输出。 - Tasos
显示剩余10条评论
2个回答

2
如果您的Activity有xml布局并且调用了setContentView(int resId),请不要通过LayoutInflater获取视图。只需找到您的根视图并设置背景即可。
FrameLayout layout = (FrameLayout) findViewById(...);
layout.setBackgroundResource(images[rand.nextInt(images.length)]);

如果你想通过LayoutInflater获取视图:
LayoutInflater inflater = getLayoutInflater();
FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.activity_my, null);
layout.setBackgroundResource(images[rand.nextInt(images.length)]);
setContentView(layout);

我使用了你的第一个建议,而不是LayoutInflater,它非常有效。谢谢! - faerubin

0

试试这个:

LayoutInflater inflater = getLayoutInflater();
FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.activity_my, null);
int[] images = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4};
Random rand = new Random();

final int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
    layout.setBackgroundDrawable(getResources().getDrawable(images[rand.nextInt(images.length)]));
} else {
    layout.setBackground(getResources().getDrawable(images[rand.nextInt(images.length)]));
}

谢谢,但我已经尝试过了,仍然出现黑色背景。 - faerubin
@faerubin 我编辑了答案。也试试那个。希望能帮到你! - SpiralDev
我将编辑我的原始问题以澄清这一点,但是为了您的利益 - 我将我的最低API级别定义为15,并且else下的逻辑需要至少16,因此我的代码无法编译。 - faerubin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接