2013-04-16 2 views
5

내 응용 프로그램에서 사용해야하는 사용자 정의 Viewgroup을 만들었지 만, ScrollView에 넣어야합니다. 레이아웃이 내 사용자 정의로만 이루어질 때 ViewGroup 모든 것이 잘 작동하지만, 이것을 ScrollView에 넣으면 아무 것도 볼 수 없습니다. 내 레이아웃 :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> 

내 뷰 그룹 :

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 : 내 CustomLayout 클래스 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)); 

    } 

} 

UPDATE : 미안하지만 난 다른 시도를했고이 경우처럼 보인다 나는 viewMember 메서드를 사용하여 viewModel에 viewGroup을 가지고 있는데, heightMeasureSpec = 0이면 다른 뷰에 뷰 그룹을 놓으면, 정수가있어. 어쩌면이게 도움이 될까요? 또한 오른쪽 아래에이 생성자를 사용하는 layout_height

답변

6

I를주는 시도가 그것을 얻었습니다. Viewgroup을 직접 측정해야했습니다. 그렇지 않으면 전혀 키가 없습니다.
그래서 : 작동 나를 위해 지금

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로 뷰를 생성 할 때 호출되는 get입니다.

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

, 아니 그 결과 다음과 같은 코드 아래에 추가합니다. – DLock

0

를 match_parent하기 :

+0

예, CustomView에 해당 생성자가 있습니다. – DLock

+0

CustomView 클래스를 게시해도 좋을지 모르겠지만, 현재 잘못되었습니다. – 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 0 픽셀을 해결하는 것이다 - 기본 최소 높이 당신이 구성 요소에있어

+0

아직 아무 것도 보이지 않습니다. – DLock

+0

사용자 지정 레이아웃 드로잉에 무엇이 있습니까? 보기에서 android : background = "@ android : color/black"설정을 시도해보십시오 - 그러면 블랙 박스가 생깁니 까? – 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을하고

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

사용하는 것이 시도이

<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> 
관련 문제