2011-01-29 43 views
14

所以我读过关于设置窗口背景和percieved性能,并正在试图仿效该罗曼盖伊的blog post。它是这样一个简单的解决方案,不知道为什么我不能得到这个工作,但活动只是拒绝回升定向背景。安卓上推出的活动设置窗口背景

我有onListItemClick启动一个新的活动,即需要3-5秒完全加载一个ListView。当用户在等待时,我想绘制一个windowBackground,以便他们在实际准备好之前“看到”活动。这里是我的代码:

为启动的活动

AndroidManifest片段:

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

为EditorActivity的XML布局:

<?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> 

最后,绘制在清单设定,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是位于int drawable folde中的.png文件河

最终的结果是EditorActivity被推出,而我看到的是默认的黑色背景与白色显示的“编辑器”文本(我补充说,测试的XML文件被正确加载。

我我也尝试通过android:background =“@ android:color/transparent”将FrameLayout和TextView的背景设置为透明,认为他们可能默认为黑色背景,但没有运气。

这已经很长了没几天,我敢肯定,我失去了一些东西简单...任何明显的失误,我在这儿举行?

+1

已更新后引用链接:http://和roid-developers.blogspot.com/2009/03/window-backgrounds-ui-speed.html – 2013-05-02 20:21:47

回答

37

在以编程方式启动新活动之前尝试设置窗口背景。

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

工作对我来说,如果我在styles.xml宣布这一个主题风格:

<?xml version="1.0" encoding="utf-8"?> 
<resources xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <style name="CustomTheme" parent="android:Theme.Holo.NoActionBar"> 
     <item name="android:windowBackground">@color/bg_evening</item> 
    </style> 

</resources> 

,并在清单我设定这个主题我的应用程序:

<application 
    android:name="com.my_app.pack" 
    android:theme="@style/CustomTheme" > 
... 

或到我的活动之一:

<activity 
    android:name="..." 
    android:theme="@style/CustomTheme"> 
... 

你可以声明muliple主题风格,并添加其中任何一个你的任何活动,这将覆盖主题设定为应用

2

我已经遇到了同样的问题,浪费了它我的一天。我们不能在java中设置主题,因为我的情况是控制权很晚才开始创建我的飞溅活动。直到那时黑屏保持可见。

这给了我线索,窗口必须从我们在清单中指定主题进行管理。

还有就是明显的,我有:

<activity 
     android:name=".main.activities.SplashScreen" 
     android:theme="@style/Splash" 
     android:screenOrientation="portrait"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
</activity> 

现在我创建的主题是:其中包含位图资源绘制资源

<style name="Splash" parent="@style/Theme.AppCompat.Light"> 
    <item name="android:windowBackground">@drawable/splash</item> 
    <item name="android:windowNoTitle">true</item> 
    <item name="windowNoTitle">true</item> 
    <item name="colorPrimaryDark">@color/green_09</item> 
    <item name="colorPrimary">@color/green_09</item> 
    <item name="windowActionBar">false</item> 
</style> 

飞溅,我必须调整一个以使它看起来完美不被拉伸并居中:

<bitmap xmlns:android="http://schemas.android.com/apk/res/android" 
    android:antialias="true" 
    android:dither="true" 
    android:gravity="fill" 
    android:src="@drawable/splash_screen" />