2010-03-25 29 views
5

Gần đây tôi đã vật lộn với bố cục Android đơn giản dường như: Tôi muốn có một số WebView phía trên một số Button. Nó hoạt động tốt với các thông số sau:LinearLayout của một WebView và một Nút

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

Button: 
    Height: wrap-content 
    Weight: unset 

Tuy nhiên, nếu trang web quá lớn, nó tràn ra ngoài nút. Tôi đã thử các kết hợp khác nhau của trọng lượng và chiều cao, và tất cả ngoại trừ một trong hai hoàn toàn ẩn nút, hoặc một phần che nó. Đây là hình thức hoạt động (được sao chép từ 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 

Nếu bạn thay đổi bất kỳ thứ gì trong số đó, ví dụ: đặt trọng số cho nút hoặc thay đổi chiều cao WebView thành nội dung bọc sau đó nó không hoạt động. Câu hỏi của tôi là: Tại sao? Ai đó có thể vui lòng giải thích những gì trên trái đất hệ thống bố trí android đang suy nghĩ?

Trả lời

2

Nội dung như sau sẽ cung cấp cho bạn những gì bạn muốn. Điều quan trọng là layout_height = "fill_parent" và layout_weight = "1" cho WebView.

<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> 


Sửa: Rất tiếc, tôi hiểu lầm câu hỏi của bạn. Đó là layout_weight mà làm cho nó không tràn qua nút (hoặc textview trong ví dụ của bạn). Tôi không chắc chắn lý do tại sao điều đó xảy ra, nhưng nếu có một mục "fill_parent" trong LinearLayout của bạn, ngoài một hoặc nhiều mục "wrap_content", bạn cần chỉ định layout_weight cho mục "fill_parent" hoặc nó sẽ mất trên không gian cho phần còn lại của các vật dụng.

+3

'fill_parent' sẽ chiếm toàn bộ không gian còn lại trong' LinearLayout', vì vậy bạn không thể có tiện ích con sau nó. Về tổng thể, đối với các bố trí như thế này, tôi đề nghị một 'RelativeLayout', vì các quy tắc rõ ràng hơn và do đó dễ bảo trì hơn (bạn không cần phải nhớ các thủ thuật' layout_weight' ma thuật). Chỉ cần có 'Nút' là' alignParentBottom' và có 'WebView' là' alignParentTop và phía trên 'Nút'. – CommonsWare

+0

Còn trường hợp bạn có 4 hoặc 5 widget thì hai trường hợp đầu tiên là "wrap_content", thứ ba là "fill_parent" và hai trường hợp cuối cùng là "Wrap_content". Điều này sẽ là một nỗi đau để duy trì với RelativeLayout - đặc biệt nếu bạn thêm một widget khác giữa hai trong số các widget khác. Với LinearLayout, bạn phải nhớ cung cấp cho một widget "có thể phát triển" "fill_parent". Bạn chỉ cần nhớ để cung cấp cho nó "trick" layout_weight "". – synic

+0

'RelativeLayout' cũng sẽ không khó đối với trường hợp đó. Tiện ích hàng đầu sẽ là 'alignParentTop', thứ hai sẽ' layout_below' đầu tiên và hai phần dưới cùng sẽ được thực hiện tương tự nhưng được căn chỉnh ở phía dưới. Tiện ích "có thể phát triển" ở giữa sẽ chỉ phải 'layout_below' tiện ích con thấp hơn trong nhóm trên cùng và' layout_above' tiện ích con trong nhóm dưới cùng. – jqpubliq

-1

bạn có thể chỉ định heigth của WebView của bạn bằng pixel

android:layout_heigth = " 70px" 

ví dụ

hy vọng nó sẽ giúp

-1

Bạn có thể thử một cái gì đó như thế này:

<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

Ok, đây là mã để thực hiện điều này:

WebView with Options Menu on Top and Bottom

Hình trên có 2 hàng nút: một ở trên và một hàng ở dưới cùng. Ở giữa là WebView. Dưới đây là tài khoản GitHub của tôi, nơi u có thể tải về mã nguồn: https://github.com/GeneChuang1/Android/tree/Android

Nếu không, đây là sự cố App:

Java Mã (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; 
     } 
    } 
} 

Tôi có 2 bố trí XML. Một cho trang web chính, cái còn lại là một trình đơn được tạo sẵn mà tôi ở trang web chính. Bố cục 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> 

Bố cục XML khác là “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 Manifest (chỉ trong trường hợp ai đó quên sự cho phép 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> 

Một lần nữa, đây là tài khoản GitHub của tôi, nơi u có thể tải về mã nguồn: https://github.com/GeneChuang1/Android/tree/Android

-Gene Chuang

1

Tôi đã hiểu rõ điều này. Cách hệ thống bố cục android hoạt động là:

  1. Tất cả mọi thứ được trình bày theo chiều cao/chiều rộng được chỉ định của chúng.
  2. Bất kỳ còn lại trọng lượng được phân phối trong số các lượt xem theo trọng số của chúng.

(Rõ ràng đây không bao giờ được giải thích.)

Vì vậy để làm cho nó làm việc bạn muốn nút được bọc nội dung, mà làm cho nó chỉ lớn như nó cần phải được, và webview để có chiều cao bằng không (vì nó có thể co lại thành 0). Sau bước 1, bạn sẽ có nút chính xác và sau đó là chế độ xem web không có chiều cao.

Sau đó, bạn đặt trọng số của nút là 0 và trọng lượng của chế độ xem web thành 1, để mọi khoảng trống còn lại được cung cấp cho chế độ xem web - tức là nó mở rộng để lấp đầy màn hình.

Làm cho cảm giác hoàn hảo khi bạn biết thuật toán.

Các vấn đề liên quan