2011-01-29 69 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" />