Android:在启动的活动上设置窗口背景 [英] Android: Setting Window Background on launched Activity

查看:20
本文介绍了Android:在启动的活动上设置窗口背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

So I've read Romain Guy's blog post on setting the Window background and percieved performance, and am trying to emulate that. It is such a simple solution and not sure why I can't get this working, but the activity simply refuses to pick-up the directed background.

I have a ListView that onListItemClick launches a new Activity, one that takes 3-5 seconds to fully load. While the user is waiting, I'd like to draw a windowBackground so that they 'see' the activity before it is actually ready. Here's my code:

AndroidManifest snippet for the launched Activity:

<activity 
        android:name=".activity.EditorActivity"
        android:screenOrientation="portrait"
        android:windowBackground="@drawable/background_editor">

The XML layout for the EditorActivity:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView 
    android:id="@+id/editor"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:text="Editor" />
</FrameLayout>

And finally, the drawable being set in the Manifest, background_editor.xml:

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/editor_bg"
    android:tileMode="repeat" />

editor_bg is a .png file located int he drawable folder.

The end result is the EditorActivity gets launched, and all I see is the default black background with the "Editor" text displayed in white (I added that to test that the XML file was loading correctly.

I've also tried setting the background of the FrameLayout and TextView to transparent via android:background="@android:color/transparent", thinking maybe they were defaulting to a black background, but no luck.

It's been a long few days, I'm sure I am missing something simple... any obvious mistakes I am making here?

解决方案

Try setting window background before launching the new activity programmatically.

getWindow().setBackgroundDrawableResource(R.drawable.my_drawable);

这篇关于Android:在启动的活动上设置窗口背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆