我做了一个自定义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 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));
}
}
更新 2:对不起,但我做了一些其他尝试,看起来如果我在 onMeasure 方法的滚动视图中有视图组,我得到 heightMeasureSpec = 0,然后如果我将视图组放在任何其他布局中,我得到一个整数。也许这会有所帮助?