2010-03-25 90 views
5

我最近苦於一個顯然簡單的Android佈局:我想要一個WebView高於Button。它與以下參數很好地工作:WebView和按鈕的LinearLayout

WebView: 
    Height: wrap-content 
    Weight: unset (by the way, what is the default?) 

Button: 
    Height: wrap-content 
    Weight: unset 

但是,如果網頁變得太大,它溢出了按鈕。我嘗試了各種重量和高度的組合,除了一個完全隱藏按鈕或者部分覆蓋按鈕之外,其他都完成了。這是工作的一個(從http://code.google.com/p/apps-for-android/source/browse/trunk/Samples/WebViewDemo/res/layout/main.xml複製):

WebView: 
    Height: 0 
    Weight: 1 

Button: 
    Height: wrap-content 
    Weight: unset 

如果更改任何這些,例如給按鈕一個重量或將WebView高度更改爲包裝內容,則它不起作用。我的問題是:爲什麼?有人能解釋一下android佈局系統在想什麼嗎?

回答

2

像下面的東西應該給你你想要的。關鍵是WebView的layout_height =「fill_parent」和layout_weight =「1」。

<LinearLayout android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
     <WebView android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" /> 

     <Button android:layout_width="fill_parent" 
       android:layout_height="wrap_content" /> 
</LinearLayout> 


編輯:哎呦,我誤解你的問題。這是layout_weight,它不會溢出按鈕(或您的示例中的textview)。我不確定它爲什麼會發生,但如果LinearLayout中有一個「fill_parent」項目,除了一個或多個「wrap_content」項目之外,還需要爲「fill_parent」項目指定layout_weight,否則將需要在其餘部件的空間上。

+3

'fill_parent'將佔用'LinearLayout'中的所有剩餘空間,所以你不能在它之後擁有小部件。總體而言,對於這樣的佈局,我推薦一個'RelativeLayout',因爲規則更明確,因此更容易維護(您不必記住魔術'layout_weight'技巧)。只需將'Button'設置爲'alignParentBottom',並將WebView設置爲'alignParentTop並且在'Button'之上。 – CommonsWare 2010-03-26 00:02:42

+0

如果您有4個或5個小部件,前兩個是「wrap_content」,第三個是「fill_parent」,最後兩個是「Wrap_content」。使用RelativeLayout來維護會很痛苦 - 特別是如果您在兩個其他小部件之間添加另一個小部件。使用LinearLayout,您必須記住給出一個「可擴展」小部件「fill_parent」。你只需要記住也給它「layout_weight」「技巧」。 – synic 2010-03-26 00:56:51

+0

'RelativeLayout'對於這種情況也不會有困難。頂部小部件將是'alignParentTop',第二個'layout_below'第一個和最後兩個將以類似方式完成,但與底部對齊。中間的「可擴展」小部件只需要在頂部組中「layout_below」下面的小部件和底部組中的「layout_above」小部件。 – jqpubliq 2010-03-26 05:17:28

-1

您可以指定的網頁視圖的heigth像素

android:layout_heigth = " 70px" 

例如

希望它有助於

-1

你可以嘗試這樣的事情:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="match_parent" > 

     <WebView 
       android:id="@+id/wv" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent" 
       android:layout_above="@+id/btn" /> 

     <Button 
      android:id="@+id/btn" 
      android:layout_alignParentBottom="true" /> 
</RelativeLayout> 
-1

好,這裏是代碼來做到這一點:

WebView with Options Menu on Top and Bottom

上面的圖片有2排按鈕:一個在上面,一個在下面。中間是WebView。 這裏是我的GitHub帳戶,其中u可以下載源代碼: https://github.com/GeneChuang1/Android/tree/Android

否則,這裏是應用程序崩潰:

Java代碼(AndroidMobileAppSampleActivity.java):

package com.gene; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 
import android.webkit.WebViewClient; 

import com.gene.R; 

public class AndroidMobileAppSampleActivity extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.app_page); 

     WebView mainWebView = (WebView) findViewById(R.id.webcontent); 

     WebSettings webSettings = mainWebView.getSettings(); 
     webSettings.setJavaScriptEnabled(true); 

     mainWebView.setWebViewClient(new MyCustomWebViewClient()); 
     mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); 

     mainWebView.loadUrl("http://www.google.com"); 
    } 

    private class MyCustomWebViewClient extends WebViewClient { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return true; 
     } 
    } 
} 

我有2個XML佈局。一個用於主網頁,另一個是我在主網頁中的預製菜單。 的XML佈局「app_page.xml」:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 
    <LinearLayout 
     android:id="@+id/page_weekly_items_options_menu" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:background="#d4dbe1" 
     android:gravity="center" 
     android:orientation="horizontal"> 

     <ImageView 
      android:id="@+id/share" 
      android:layout_width="60dp" 
      android:layout_height="50dp" 
      android:background="@drawable/icon" 
      android:clickable="true" 
      android:onClick="userClickedShare"></ImageView> 

     <ImageView 
      android:id="@+id/left_arrow" 
      android:layout_width="60dp" 
      android:layout_height="50dp" 
      android:background="@drawable/icon" 
      android:clickable="true" 
      android:onClick="userClickedLeftArrow"></ImageView> 

     <ImageView 
      android:id="@+id/right_arrow" 
      android:layout_width="60dp" 
      android:layout_height="50dp" 
      android:background="@drawable/icon" 
      android:clickable="true" 
      android:onClick="userClickedRightArrow"></ImageView> 

     <ImageView 
      android:id="@+id/notifications_pageweeklyitem" 
      android:layout_width="60dp" 
      android:layout_height="50dp" 
      android:background="@drawable/icon" 
      android:clickable="true" 
      android:onClick="userClickedNotificationsPageWeeklyItem"></ImageView> 

     <ImageView 
      android:id="@+id/favorites_pageweeklyitem" 
      android:layout_width="60dp" 
      android:layout_height="50dp" 
      android:background="@drawable/icon" 
      android:clickable="true" 
      android:onClick="userClickedFavoritesPageWeeklyItem"></ImageView> 

    </LinearLayout> 

    <RelativeLayout 
     android:id="@+id/webcontent_container" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_below="@id/page_weekly_items_options_menu"> 

     <WebView 
      android:id="@+id/webcontent" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_above="@+id/menu" 
      ></WebView> 
     <include 
      android:id="@+id/menu" 
      layout="@layout/bottom_menu" 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      android:layout_alignParentBottom="true" 
      android:gravity="bottom" 
      android:layout_weight="1" 
      /> 


    </RelativeLayout> 

</RelativeLayout> 

其它XML佈局「bottom_menu。XML」:

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/bottom_scroll_menu" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_gravity="bottom" > 
    <!-- This layout is used by activity_main.xml. 
    It is part of the main home page --> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="#17528c" 
     android:orientation="horizontal" > 

     <ImageView 
      android:id="@+id/Weekly" 
      android:layout_width="60dp" 
      android:layout_height="50dp" 
      android:background="@drawable/icon" 
      android:clickable="true" 
      android:onClick="userClickedWeekly" > 
     </ImageView> 

     <ImageView 
      android:id="@+id/Search" 
      android:layout_width="60dp" 
      android:layout_height="50dp" 
      android:background="@drawable/icon" 
      android:clickable="true" 
      android:onClick="userClickedSearch" > 
     </ImageView> 

     <ImageView 
      android:id="@+id/Favorites" 
      android:layout_width="60dp" 
      android:layout_height="50dp" 
      android:background="@drawable/icon" 
      android:clickable="true" 
      android:onClick="userClickedFavorites" > 
     </ImageView> 

     <ImageView 
      android:id="@+id/Notifications" 
      android:layout_width="60dp" 
      android:layout_height="50dp" 
      android:background="@drawable/icon" 
      android:clickable="true" 
      android:onClick="userClickedNotifications" > 
     </ImageView> 

     <ImageView 
      android:id="@+id/Profile" 
      android:layout_width="60dp" 
      android:layout_height="50dp" 
      android:background="@drawable/icon" 
      android:clickable="true" 
      android:onClick="userClickedProfile" > 
     </ImageView> 

     <ImageView 
      android:id="@+id/About" 
      android:layout_width="60dp" 
      android:layout_height="50dp" 
      android:background="@drawable/icon" 
      android:clickable="true" 
      android:onClick="userClickedAbout" > 
     </ImageView> 
    </LinearLayout> 

</HorizontalScrollView> 

Android清單(只是櫃面有人忘記Internet權限):

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="tscolari.mobile_sample" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="10" /> 

    <uses-permission android:name="android.permission.INTERNET"/> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".AndroidMobileAppSampleActivity" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 
</manifest> 

同樣,這裏是我的GitHub帳戶,其中u可以下載源代碼: https://github.com/GeneChuang1/Android/tree/Android

-Gene Chuang

1

我自從就明白了這一點。 Android佈局系統的工作方式是:

  1. 所有的東西都根據它們指定的高度/寬度進行佈置。
  2. 任何剩餘權重根據權重在視圖之間分配。

(顯然這是從來沒有解釋。)

因此,要得到它的工作你想要的按鈕是總結的內容,因爲它需要這使得它有這麼大,和web視圖來爲零高度(因爲它可以縮小到零)。在步驟1之後,您將擁有正確的按鈕,然後是webview零高度。

然後,您將按鈕權重設置爲0,並將webview權重設置爲1,以便將任何剩餘空間提供給webview - 即將其展開以填充屏幕。

當你知道該算法時,這是非常有意義的。