2017-05-11 16 views
6

Câu hỏi này đã được hỏi here, nhưng không có giải pháp.Tại sao thuộc tính minHeight không hoạt động trong WebView Android?

Tôi có một số WebView. Tôi muốn đặt chiều cao tối thiểu là WebView bằng thuộc tính minHeight nhưng không hoạt động. Các thuộc tính tương tự hoạt động cho Button.

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
tools:context="com.example.anshul.webview.WebActivity"> 

<WebView 
    android:id="@+id/webView" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="400dp"></WebView> 

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:minHeight="150dp" 
    android:text="This is a Button. It's minHeight is set to 150 dp and it works !!"/> 

Rõ ràng từ hình ảnh dưới đây, WebView không hỗ trợ thuộc tính minHeight. Có ai biết một giải pháp cho vấn đề này?

enter image description here

+0

trong đó phiên bản Android bạn gặp vấn đề này? – savepopulation

+0

Tôi đã thử nó trên Android 25 và phải đối mặt với vấn đề này. Nhưng tôi nghĩ nó không liên quan đến phiên bản Android. – thedarkpassenger

+0

có một số vấn đề về webview trong các phiên bản kat trước bộ. vì vậy tôi hỏi. – savepopulation

Trả lời

7

Trước tiên, hãy hiểu xem cách khác sử dụng android:minHeight thuộc tính. Hãy lấy ví dụ Spinner. Trong AbsSpinner#onMeasure() chúng tôi mã nhìn thấy đoạn mã sau đây:

... 
preferredHeight = Math.max(preferredHeight, getSuggestedMinimumHeight()); 
preferredWidth = Math.max(preferredWidth, getSuggestedMinimumWidth()); 

heightSize = resolveSizeAndState(preferredHeight, heightMeasureSpec, 0); 
widthSize = resolveSizeAndState(preferredWidth, widthMeasureSpec, 30); 

setMeasuredDimension(widthSize, heightSize); 
... 

Vì vậy, getSuggestedMinimumHeight() nên được coi khi tính toán chiều cao ưa thích.

Bây giờ, hãy xem cách đo lường WebView.

AwLayoutSizer là thành phần cuối cùng đó là trách nhiệm đo WebViewwe can clearly see, mà onMeasure() nó không tôn trọng getSuggestedMinimumHeight() v alue.

Tôi không chắc chắn đây có phải là hành vi dự định hay không. Tuy nhiên, tôi không thể tìm thấy enough seams bằng cách nào đó ảnh hưởng đến quá trình đo lường đó. Here's mâm cặp mã trong lớp WebView, trong đó đối tượng cuối cùng sẽ trả về WebViewChromium (bước đầu tiên trong thứ tự nêu trên) được khởi tạo.

 

    private void ensureProviderCreated() { 
     checkThread(); 
     if (mProvider == null) { 
      // As this can get called during the base class constructor chain, pass the minimum 
      // number of dependencies here; the rest are deferred to init(). 
      mProvider = getFactory().createWebView(this, new PrivateAccess()); 
     } 
    } 
 

Như bạn có thể thấy, đây không phải là thứ có thể dễ dàng tùy chỉnh/thay đổi.

-1

Thử:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     WebView webView = (WebView)findViewById(R.id.webView); 
     webView.loadDataWithBaseURL(null, "<html><body bgcolor=\"#E6E6FA\"> hehehe </body></html>", "text/html", "utf-8", null); 

    } 



<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:background="@color/blue" 
    android:layout_height="match_parent"> 
    <LinearLayout 
     android:minHeight="300dp" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" 
     > 
     <WebView 
      android:id="@+id/webView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"/> 
    </LinearLayout> 


    <Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:minHeight="150dp" 
     android:text="This is a Button. It's minHeight is set to 150 dp and it works !!"/> 
    </RelativeLayout> 

enter image description here

+0

Bạn nên tính đến [cảnh báo] (http://i.imgur.com/v1OfFzl.png), mà lint đưa ra. – azizbekian

+0

Chúng ta có thể lấy nó bằng mã. xác định chiều cao cho LinearLayout. – MrTy

0

Tôi nghĩ rằng kích thước webview làm thêm theo thuộc tính view cảng nội dung ...

Kiểm tra https://developer.android.com/guide/webapps/targeting.html#Viewport

Chế độ xem là khu vực trang web của bạn được vẽ. Mặc dù tổng diện tích hiển thị của chế độ xem phù hợp với kích thước của màn hình khi thu phóng tất cả các lối ra, chế độ xem có kích thước pixel riêng của nó mà nó cung cấp cho trang web.Ví dụ: mặc dù màn hình thiết bị có thể có chiều rộng là 480 pixel, nhưng chế độ xem có thể có chiều rộng 800 pixel. Điều này cho phép trang web được thiết kế rộng 800 pixel để hiển thị hoàn toàn trên màn hình khi tỷ lệ khung nhìn là 1.0. Hầu hết các trình duyệt web trên Android (bao gồm cả Chrome) đặt chế độ xem thành kích thước lớn theo mặc định (được gọi là "chế độ khung nhìn rộng" rộng khoảng 980px). Nhiều trình duyệt cũng thu nhỏ càng nhiều càng tốt, theo mặc định, để hiển thị chiều rộng khung nhìn đầy đủ (được gọi là "chế độ tổng quan").

0

sử dụng bố cục ràng buộc. Điều này sẽ giúp giải quyết tất cả các loại lỗi này và rất dễ sử dụng.

nếu phiên bản studio android của bạn thấp hơn 2.3.1, sau đó thêm sự phụ thuộc này.

compile 'com.android.support.constraint:constraint-layout:1.0.0-beta1' 
Các vấn đề liên quan