2011-08-31 10 views
5

Trong một Hoạt động có nhiều thành phần, tôi có một RelativeLayout có một WebView (nó hiển thị một văn bản đơn giản, mà tôi không biết kích thước của nó).Điều chỉnh kích thước bố cục động trong Android

Đây là mã xml:

<RelativeLayout android:id="@+id/descripcionRelative" android:layout_below="@+id/descripcion_bold" android:layout_width="match_parent" android:layout_height="125dip" android:background="@drawable/my_border"> 
    <WebView android:id="@+id/webViewDescripcion" android:layout_width="wrap_content" android:layout_height="wrap_content"/>  
</RelativeLayout> 

Các android thuộc tính: layout_height của RelativeLayout là 125dip, bởi vì nếu văn bản là quá lớn, tôi muốn phân định để 125dip. Nếu văn bản lớn, tôi sẽ thấy văn bản có cuộn. Tuyệt quá!

Nhưng ... nếu văn bản ngắn, tôi thấy rất nhiều không gian cần thiết.

Một giải pháp là thay đổi android: layout_height của RelativeLayout thành wrap_content. Nếu văn bản ngắn, thành phần sẽ có pixel chính xác, nhưng nếu văn bản quá lớn, tôi không thể phân định nó.

VẤN ĐỀ LỚN là tôi không thể tính chiều cao của WebView. Nếu tôi làm: descripcion_web.getHeight() nó trả về 0.

Nếu tôi gọi phương thức này ở đây, nó không trả về số tốt:

descripcion_web.setWebViewClient(new WebViewClient() { 
     @Override 
     public void onPageFinished(WebView webView, String url) { 
      super.onPageFinished(webView, url); 
      RelativeLayout marco = (RelativeLayout)findViewById(R.id.descripcionRelative); 
      System.out.println("Height webView: "+webView.getHeight()); 
      System.out.println("Height relative: "+marco.getHeight());    
     }   
    }); 

tôi cố gắng gọi phương thức trong onResume() nhưng nó không hoạt động.

Một nỗ lực khác để giải quyết sự cố, được đặt android: layout_height thành match_parent và sử dụng phương thức Xem setMaximumHeight(), nhưng không tồn tại. Tuy nhiên, tồn tại setMinimumHeight() ...

Làm cách nào để giải quyết vấn đề này? Cảm ơn nhiều!

Trả lời

7

Thật không may, không có hộp "setMaximumHeight" không có trong hầu hết các chế độ xem Android. Nhưng bạn có thể thực hiện kế thừa từ WebView. Dưới đây là ví dụ về cách bạn có thể làm điều đó:

package com.am.samples.maxheight; 

import android.content.Context; 
import android.util.AttributeSet; 
import android.webkit.WebView; 

public class CustomWebView extends WebView { 

    private int maxHeightPixels = -1; 

    public CustomWebView(Context context, AttributeSet attrs, int defStyle, 
      boolean privateBrowsing) { 
     super(context, attrs, defStyle, privateBrowsing); 
    } 

    public CustomWebView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public CustomWebView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public CustomWebView(Context context) { 
     super(context); 
    } 

    public void setMaxHeight(int pixels) { 
     maxHeightPixels = pixels; 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     if (maxHeightPixels > -1 && getMeasuredHeight() > maxHeightPixels) { 
      setMeasuredDimension(getMeasuredWidth(), maxHeightPixels); 
     } 
    } 
} 

Hy vọng điều này sẽ hữu ích!