2011-08-31 13 views
5

कई घटकों के साथ एक गतिविधि में, मेरे पास एक रिलेवेटिवआउट है जिसमें वेबव्यू है (यह एक साधारण पाठ दिखाता है, जिसे मैं उसका आकार नहीं जानता)।एंड्रॉइड में लेआउट के गतिशील आकार को समायोजित करना

इस एक्सएमएल कोड है:

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

विशेषता एंड्रॉयड: RelativeLayout की layout_height, 125dip है क्योंकि अगर पाठ बहुत बड़ा है, मैं 125dip को हदबंदी करना चाहते हैं। यदि टेक्स्ट बड़ा है, तो मुझे स्क्रॉल के साथ टेक्स्ट दिखाई देगा। महान!

लेकिन ... यदि पाठ छोटा है, तो मुझे बहुत सारी अनावश्यक जगह दिखाई देती है।

एक समाधान एंड्रॉइड बदल रहा है: wrap_content के relativeLayout का layout_height। यदि पाठ छोटा है, तो घटक में सटीक पिक्सल होंगे, लेकिन यदि टेक्स्ट बहुत बड़ा है, तो मैं इसे सीमित नहीं कर सकता।

बड़ी समस्या यह है कि मैं वेबव्यू की ऊंचाई की गणना नहीं कर सकता। तो मुझे क्या: descripcion_web.getHeight() यह 0.

वापसी अगर मैं इस विधि यहाँ कहते हैं, यह अच्छी तरह से संख्या वापस नहीं करता है:

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());    
     }   
    }); 

मैं onResume में विधि कॉल करने की कोशिश(), लेकिन यह काम नहीं करता है।

समस्या को हल करने का एक और प्रयास, एंड्रॉइड सेट है: layout_height match_parent के लिए और एक व्यू विधि सेट मैक्सिमहाइट() का उपयोग करें, लेकिन यह अस्तित्व में नहीं है। हालांकि, setMinimumHeight() ...

मैं इसे कैसे हल कर सकता हूं? आपका बहुत बहुत धन्यवाद!

उत्तर

7

दुर्भाग्य से अधिकांश एंड्रॉइड दृश्यों के लिए "setMaximumHeight" बॉक्स से बाहर नहीं है। लेकिन आप WebView से ऐसी विरासत को कार्यान्वित कर सकते हैं। यहां एक उदाहरण दिया गया है कि आप यह कैसे कर सकते हैं:

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); 
     } 
    } 
} 

आशा है कि इससे मदद मिलती है!

संबंधित मुद्दे