我制作了一个自定义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>
我的观点组:
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 b) { // Todo Auto-generated method stub //do something and call layout on every child } protected void onMeasure(int widthMeasureSpec,heightMeasureSpec); /* do something and call for each child View v = getChildAt(i); v.measure(wspec,hspec); */ setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(),heightMeasureSpec)); } }
更新2:
抱歉,我做了一些其他的尝试,如果我在onMeasure方法的滚动视图中有viewgroup,我得到heightMeasureSpec = 0,然后如果我把viewgroup放在任何其他布局,我得到一个整数.也许这会有所帮助?
解决方法
我明白了,我不得不自己测量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(),getDefaultSize(this.getSuggestedMinimumHeight(),heightMeasureSpec));
现在对我来说它有效.