2013-04-16 11 views
5

Ho creato un numero personalizzato Viewgroup che ho bisogno di usare nella mia applicazione, ma ho bisogno di inserirlo in un ScrollView. Quando il layout è fatto solo con la mia custom ViewGroup, tutto funziona correttamente, ma quando lo metto in un ScrollView non riesco a vedere nulla. mio layout:ScrollView rende invisibile un layout personalizzato

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

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

UPDATE: La mia classe 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)); 

    } 

} 

UPDATE 2: dispiace ma ho fatto qualche altro cerca e sembra che se Ho il viewgroup in una scrollview sul metodo onMeasure che ho ottenuto heightMeasureSpec = 0, quindi se metto il viewgroup in qualsiasi altro layout, ho un intero Forse questo sarebbe di aiuto?

risposta

6

I capito, ho dovuto misurare il Viewgroup da solo, altrimenti non ho avuto altezza.
Quindi:

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

Ora, per me funziona.

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

tenta di modificare Android: layout_height per match_parent

+0

provato, senza risultato. – DLock

0

Si utilizza anche questo contructor sotto, a destra? Perché questo è quello che viene chiamato quando crei la tua vista in xml.

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

Sì, ho quel costruttore nel mio CustomView – DLock

+0

Ti dispiacerebbe pubblicare la tua classe CustomView, è difficile dire per ora cosa c'è che non va. – yahya

+0

Ho aggiornato la mia domanda – DLock

0

Stai usando wrap_content per l'altezza:

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

in modo da risolvere per quanto ne sappia io 0px in quanto non v'è alcun contenuto - provare a dare sei componente un'altezza minima di default

+0

Fatto che, ancora non vedo nulla – DLock

+0

Il tuo layout personalizzato disegna qualcosa? Prova a impostare android: background = "@ android: color/black" sulla tua vista - ti dà una scatola nera? – Graeme

+0

Sì, c'è la scrollview, diventa tutto nero se metto quello sfondo. – DLock

0

Controlla che funzionerebbe perfettamente!

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


} 

fare attr.xml nella cartella valori e aggiungere il codice qui sotto

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

Usa come questo

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