2010-03-25 92 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 - 即将其展开以填充屏幕。

当你知道该算法时,这是非常有意义的。