2013-08-05 33 views
3

我不只是在API中尋找Splash Screen功能,如果可能,我希望HTML內容使用透明背景,並依靠WebView提供背景圖片。這樣的事情可能嗎?如何在Android的Cordova/PhoneGap上的WebView上設置背景圖像?

除此之外,我是否至少可以在WebView上設置背景顏色,以便「滲透」HTML內容?

+0

但值得一讀的話題:http://cyrilmottier.com/2012/05/03/splash-screens-are-evil-dont-use-them/ –

回答

5

想通了在挖掘Cordova源文件和Android文檔後。在SRC/COM /.../ MyApp.java:

public class MyApp extends DroidGap 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     super.loadUrl(Config.getStartUrl(), 20000); 

     // Must be after loadUrl! 
     super.appView.setBackgroundColor(0x00000000); // transparent RGB 
     super.appView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null); 

     // Put background image in res/drawable/splash.jpg 
     Drawable theSplash = getResources().getDrawable(R.drawable.splash); 
     super.root.setBackground(theSplash); 
    } 
} 

而CSS:

html, 
body, 
.transparent { background-color: transparent !important; } 
+0

只是好奇你爲什麼要添加背景圖像到webview而不是html頁面?做這樣的事情似乎取消了Cordova的一個主要優點:跨平臺。 – MBillau

+0

@MBillau:獲得與'background-attachment:fixed'或'position:fixed'相同的效果,這兩者在Android的WebView環境中都存在問題。 –

+1

哦,我看到......呃,太糟糕了,沒有一種更好的HTML5方法可以實現這一點。 – MBillau

1

我不知道如何設置的圖像,但你可以使用此設置背景顏色:

<preference name="backgroundColor" value="0x00000000" /> 

您將在補充一點:RES/XML/config.xml中

+0

Android 2.3/Cordova 2.8.1上的白色背景2.8.1 ,使用空白index.html和* {background:transparent!important; }。 –

+0

嘗試將該代碼添加到位於/ www –

+0

中的config.xml文件仍然沒有骰子,嘗試在2.3和4.2。 –

1

您可能要在構建一些自動化的這一點,但是這會工作。這就是我在啓動應用程序時在Android中出現啓動畫面的原因(不是在它已經加載或有人工延遲之後)...

在platforms/android/res/values/styles.xml中創建一個文件

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="MyTheme" parent="@android:style/Theme.NoTitleBar"> 
     <!-- set the splash screen as the background image on all windows --> 
     <item name="android:windowBackground">@drawable/screen</item> 
    </style> 
</resources> 

在你的平臺/安卓/ manifest.xml中替換@的主題風格/ MyTheme的

在你的config.xml文件中添加以下兩行

<!-- make the webview transparent --> 
<preference name="backgroundColor" value="0x00000000" /> 
<!-- cordova will copy the splash screen file to screen.png, 
    but seems to ignore it after then 
--> 
<splash src="res/splash/splash.png"/> 

顯然你需要一個splash.png文件來使用。

相關問題