2013-04-16 65 views
5

Tôi đã thực hiện một tuỳ chỉnh Viewgroup mà tôi cần sử dụng trong đơn đăng ký của mình, nhưng tôi cần phải đặt nó trong một số ScrollView. Khi bố trí được thực hiện chỉ với tùy chỉnh của tôi ViewGroup tất cả mọi thứ hoạt động tốt, nhưng khi tôi đặt nó trong một ScrollView tôi không thể nhìn thấy bất cứ điều gì. bố trí của tôi:ScrollView tạo bố cục tùy chỉnh ẩn

<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 của tôi:

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: lớp CustomLayout My

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: Xin lỗi nhưng tôi thực hiện một số cố gắng khác và có vẻ như nếu Tôi có viewgroup trong một scrollview trên phương pháp onMeasure tôi đã heightMeasureSpec = 0, sau đó nếu tôi đặt viewgroup trong bất kỳ bố trí khác, tôi có một số nguyên. Có lẽ điều này sẽ giúp?

Trả lời

6

tôi hiểu rồi, tôi phải tự mình đo Viewgroup, hoặc tôi không có chiều cao nào cả.
Vì vậy:

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

Bây giờ với tôi nó hoạt động.

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

cố gắng để thay đổi android: layout_height để match_parent

+0

Cố gắng đó, kết quả không có. – DLock

0

Bạn cũng đang sử dụng contructor này dưới đây, phải không? Bởi vì đây là cái được gọi khi bạn tạo chế độ xem của mình trong xml.

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

Có, tôi có hàm tạo đó trong CustomView – DLock

+0

Bạn có nhớ đăng lớp CustomView của mình không, bây giờ khó có thể nói điều gì sai. – yahya

+0

Đã cập nhật câu hỏi của tôi – DLock

0

Bạn đang sử dụng wrap_content cho chiều cao:

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

nào sẽ giải quyết để 0px afaik như không có nội dung - cố gắng đem lại cho bạn là thành phần một chiều cao tối thiểu mặc định

+0

Đã xong, tôi vẫn không thấy bất cứ điều gì – DLock

+0

Bố cục tùy chỉnh của bạn có vẽ bất kỳ thứ gì không? Thử cài đặt android: background = "@ android: color/black" trên chế độ xem của bạn - điều này có cho bạn hộp đen không? – Graeme

+0

Có, có scrollview, nó biến tất cả màu đen nếu tôi đặt nền đó. – DLock

0

Kiểm tra điều này sẽ hoạt động hoàn hảo!

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


} 

làm attr.xml trong thư mục các giá trị và thêm mã dưới đây

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

Sử dụng như thế này

<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> 
Các vấn đề liên quan