2013-02-20 182 views
4

我試圖讓頭部「54dp」,其餘的活動是一個WebView。android webview佔用全屏

當我在eclips中的「圖形佈局」中預覽我的活動時,它看起來不錯。但是當在手機或模擬器上運行應用程序時,webview會佔用整個屏幕。

這是預覽的樣子,我怎麼想的那樣:

enter image description here

但是,這是我所看到的,當我運行的應用程序:

enter image description here

這是什麼佈局xml看起來像:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/page_background" 
    > 

    <LinearLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/Layout_Header" 
     android:layout_width="fill_parent" 
     android:layout_height="54dp" 
     android:orientation="horizontal" 
     android:background="@drawable/container_header" 
     android:paddingLeft="16dp" 
     android:paddingRight="16dp" 

     > 

     <ImageView 
      android:id="@+id/imgHeaderLogo" 
      android:layout_weight="1" 
      android:layout_width="0dp" 
      android:layout_height="46dp" 
      android:contentDescription="@string/img_header_logo_description" 
      android:src="@drawable/header_logo" 
      android:layout_gravity="center" 
      /> 


    </LinearLayout> 


    <WebView android:id="@+id/web_engine" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      /> 
</LinearLayout> 

SOLUTION:

在活動的java文件我有這樣的:

WebView browser = (WebView) findViewById(R.id.web_engine); 
WebSettings webSettings = browser.getSettings(); 
setContentView(browser); 

當我應該有這樣的:

setContentView(R.layout.activity_sign_in_web_view); 

回答

3

設置的WebView高度0dp,其重量爲1

<WebView android:id="@+id/web_engine" 
     android:layout_width="fill_parent" 
     android:layout_height="0dp" 
     android:layout_weight="1" 
     /> 
+0

我說得太快......這應該有效。但只要我把代碼加載一個頁面......它需要整個屏幕。很奇怪。 – capdragon 2013-02-21 01:11:31

+0

想通了......謝謝!給你信用,因爲使用體重填充它最好。我發佈了一個更新到我的答案。 – capdragon 2013-02-21 15:43:41

1

改變這種手動XML的Web視圖

android:layout_height="fill_parent" 

as

android:layout_height="200dp" 

如果它的工作原理改變高度參數,你在想「機器人:layout_height」

+0

同樣的事情......當我運行它時忽略了layout_height。我不知道爲什麼。 – capdragon 2013-02-21 01:25:08

+0

在你的Imageview xml中,你已經將寬度設置爲0dp,將其設置爲 - > android:layout_width =「200dp」,並檢查 – 2013-02-21 03:34:09

+0

當我這樣做時,eclipse會說「使用0dip的layout_width而不是200dp來獲得更好的性能」相同的結果... webview全屏。 :/ – capdragon 2013-02-21 15:37:37

3

這是因爲一些重定向,從而打開瀏覽器應用程序,你可以使用

webview.setWebViewClient(new WebViewClient() { 
    @Override 
    public boolean shouldOverrideUrlLoading(WebView view, String url) {   
     view.loadUrl(url); 
     return true; 
    } 
}); 

加載重定向你WebView。我剛剛有同樣的問題,並找到解決方案here

+0

神祕解決! – nont 2014-12-12 15:51:18