2013-04-16 16 views
5

Uygulamamda kullanmam gereken bir özel Viewgroup yaptım, ancak bir ScrollView içine koymam gerekiyor. Düzen yalnızca benim özel ViewGroup ile yapıldığında her şey iyi çalışıyor, ancak bir ScrollView içine koyduğumda hiçbir şey göremiyorum. Benim düzen:ScrollView, özel bir düzen görünmez yapar

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

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

GÜNCELLEME: My CustomLayout sınıfı 2

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

    } 

} 

GÜNCELLEME: Üzgünüm ama başka denemeden yapılmış ve eğer benziyor ViewMotureSpec = 0'a sahip olduğum onMeasure yönteminde bir viewview görünümüne sahibim, sonra viewgroup'u başka bir düzende yerleştirirsem, bir tamsayı var. Belki bu yardımcı olur? Ayrıca sağ aşağıya bu contructor kullandığınız layout_height

cevap

6

daha vermeyi deneyin Anladım, Viewgroup'u kendim ölçmek zorundaydım, ya da hiç yükseklik almamıştım.
Yani: çalıştığını benim için Şimdi

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

deneyin android değiştirilir? Çünkü xml'de görünümünüzü oluşturduğunuzda bu aranacak.

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

, hiçbir sonuç gibi kod aşağıda ekleyin. – DLock

0

match_parent için:

+0

Evet, CustomView – DLock

+0

içinde bu kurucu var CustomView sınıfınızı göndermek ister misiniz, şimdi yanlış olanı söylemek zor. – yahya

+0

Benim sorum – DLock

0

Sen yüksekliği için wrap_content kullandığınız: hiçbir içerik olmadığı için

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

afaik 0px için bir çözüm getireceği hangisi - varsayılan bir minimum yükseklik sen bileşen konum

+0

güncelleştirildi Hala bir şey görmüyorum Bitti – DLock

+0

Özel Düzen çiziminiz herhangi bir şey mi? Android'i ayarlamayı deneyin: görünümünüzde background = "@ android: color/black" - bu size bir kara kutu mı veriyor? – Graeme

+0

Evet, scrollview var, arka plana koyduğumda tüm siyah döner. – DLock

0

Bu iş mükemmel çalışır!

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


} 

değerleri klasöründe attr.xml yapıp

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

kullanın o çalıştı bu

<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> 
İlgili konular