2013-04-16 12 views
5

मैंने कस्टम Viewgroup बनाया है जिसे मुझे अपने एप्लिकेशन में उपयोग करने की आवश्यकता है, लेकिन मुझे इसे ScrollView में रखना होगा। जब लेआउट केवल मेरे कस्टम ViewGroup के साथ किया जाता है तो सब ठीक काम करता है, लेकिन जब मैं इसे ScrollView में डालता हूं तो मुझे कुछ भी दिखाई नहीं दे रहा है। मेरे लेआउट:स्क्रॉलव्यू एक कस्टम लेआउट अदृश्य बनाता है

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

    <com.example.test.CustomLayout 
     android:id="@+id/layout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </com.example.test.CustomLayout> 

</ScrollView> 

मेरे viewgroup:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ 

     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
      /* do something and call for each child    
        View v = getChildAt(i); 
        v.measure(wspec, hspec); 
        */ 

     setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec)); 

    } 

    protected void onLayout(boolean changed, int l, int t, int r, int b) { 
     // TODO Auto-generated method stub 
     //do something and call layout on every child 
    } 

अद्यतन: मेरे CustomLayout वर्ग

public class CustomLayout extends ViewGroup{ 

    /*My params*/ 

    public CustomLayout(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

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

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

    @Override 
    protected void onLayout(boolean changed, int l, int t, int r, int b) { 
     // TODO Auto-generated method stub 
      //do something and call layout on every child 
    } 

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){ 

     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
       /* do something and call for each child    
         View v = getChildAt(i); 
         v.measure(wspec, hspec); 
         */ 

      setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec)); 

    } 

} 

अद्यतन 2: क्षमा करें, लेकिन मैं कुछ अन्य की कोशिश करता बना दिया है और यह अगर की तरह दिखता है मेरे पास ऑनमेज़र विधि पर स्क्रॉलव्यू में व्यू ग्रुप है, मुझे ऊंचाई मिलीमीटर = 0 मिली है, तो अगर मैं व्यूग्रुप को किसी अन्य लेआउट में डालता हूं, मुझे एक पूर्णांक मिला। शायद यह मदद करेगा? layout_height match_parent को

उत्तर

6

मैं कोशिश समझ गया, मुझे खुद को व्यूग्रुप को मापना पड़ा, अन्यथा मुझे कोई ऊंचाई नहीं मिली।
तो:

int heightMeasured = 0; 
/*for each child get height and 
heightMeasured += childHeight;*/ 


//If I am in a scrollview i got heightmeasurespec == 0, so 
if(heightMeasureSpec == 0){ 
heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightMeasured, MeasureSpec.AT_MOST); 
} 

setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec), getDefaultSize(this.getSuggestedMinimumHeight(), heightMeasureSpec)); 
अब मेरे लिए

यह काम करता है।

0
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/scrollView1" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

कोशिश एंड्रॉयड बदलने के लिए? क्योंकि जब आप xml में अपना दृश्य बनाते हैं तो यह एक कहा जाता है।

public CustomLayout(Context context, AttributeSet attrs) { 
    super(context, attrs); 
     // Do your stuff 
} 
+0

कि कोशिश की, कोई परिणाम की तरह

<resources> <declare-styleable name="ScrollMaxHeight"> <attr name="maxHeight" format="integer"/> </declare-styleable> </resources> 

उपयोग कोड के नीचे जोड़ें। – DLock

0

तुम भी, ठीक नीचे इस contructor उपयोग कर रहे हैं:

+0

हां, मेरे पास मेरे कस्टम व्यू – DLock

+0

में वह कन्स्ट्रक्टर है, क्या आप अपनी कस्टम व्यू क्लास पोस्ट करना चाहते हैं, अब यह कहना मुश्किल है कि क्या गलत है। – yahya

+0

मेरा प्रश्न अपडेट किया गया – DLock

0

आप ऊंचाई के लिए wrap_content उपयोग कर रहे हैं: के रूप में कोई सामग्री है

<com.example.test.CustomLayout 
    android:id="@+id/layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" > 

जो AFAIK 0px को हल होगा - एक डिफ़ॉल्ट न्यूनतम ऊंचाई आप घटक रहे देने

+0

ऐसा किया गया, फिर भी मुझे कुछ भी नहीं दिख रहा है – DLock

+0

क्या आपका कस्टम लेआउट कुछ भी चित्रित कर रहा है? एंड्रॉइड सेटिंग करने का प्रयास करें: पृष्ठभूमि = "@ एंड्रॉइड: रंग/काला" आपके दृश्य पर - क्या यह आपको ब्लैक बॉक्स देता है? – Graeme

+0

हां, स्क्रॉलव्यू है, अगर मैं उस पृष्ठभूमि को डालता हूं तो यह काला हो जाता है। – DLock

0

जांचें यह सही काम करेगा!

public class MaxHeightScrollView extends ScrollView { 

    public static int DEFAULT_MAX_HEIGHT = -1; 
    private int maxHeight = DEFAULT_MAX_HEIGHT; 

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

    public MaxHeightScrollView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     TypedArray a = context.getTheme().obtainStyledAttributes(
       attrs, 
       R.styleable.ScrollMaxHeight, 
       0, 0); 
     try { 
      setMaxHeight(a.getInt(R.styleable.ScrollMaxHeight_maxHeight, 0)); 
     } finally { 
      a.recycle(); 
     } 
    } 

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

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     try { 
      int heightSize = MeasureSpec.getSize(heightMeasureSpec); 
      if (maxHeight != DEFAULT_MAX_HEIGHT 
        && heightSize > maxHeight) { 
       heightSize = maxHeight; 
       heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.AT_MOST); 
       getLayoutParams().height = heightSize; 

      } else { 
       heightMeasureSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.AT_MOST); 
       setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec) 
         , getDefaultSize(this.getSuggestedMinimumHeight(), heightMeasureSpec)); 
      } 

     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
     } 
    } 

    public void setMaxHeight(int maxHeight) { 
     this.maxHeight = maxHeight; 
    } 


} 

मूल्यों फ़ोल्डर में attr.xml बनाने के लिए और इस

<com.yourapppackage.customviews.MaxHeightScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    app:maxHeight="200" 
    android:fadeScrollbars="false" 
    android:gravity="center_horizontal"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 
     ........... 
     ............ 
    </LinearLayout> 

</com.yourapppackage.customviews.MaxHeightScrollView> 
संबंधित मुद्दे