android - ScrollView 使自定义布局不可见

标签 android layout viewgroup

我制作了一个自定义的 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>

我的 View 组:

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 
    }

更新: 我的自定义布局类

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 方法的 ScrollView 中有 View 组,我会得到 heightMeasureSpec = 0,然后如果我将 View 组放在任何其他布局中,我会得到一个整数。也许这会有所帮助?

最佳答案

我明白了,我必须自己测量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));

现在对我来说它有效。

关于android - ScrollView 使自定义布局不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16033329/

相关文章:

android - 为什么Android 8.1看不到资源?

jquery - 拖动调整 Bootstrap 列大小 jQuery UI

android - 如何在android中从中间折叠 View ?

java - Android 将原始源类复制到自定义类

java - 将主题应用于 AlertDialog Builder 会导致标题无法正常工作

android - 如何在我的 ListView 上启用 LongCLicks?

jquery - 修复 jQuery block 元素动画

html - 为什么顶部边距正在崩溃但底部边距却没有?

android - AIDL 无法找到 Parcelable 类的定义