6

我做了一个自定义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,然后如果我将视图组放在任何其他布局中,我得到一个整数。也许这会有所帮助?

4

5 回答 5

6

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

现在对我来说它有效。

于 2013-04-16T15:07:58.080 回答
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" >

尝试将 android:layout_height 更改为 match_parent

于 2013-04-16T09:32:04.837 回答
0

您正在使用wrap_content的高度:

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

由于没有内容,这将解析为 0px afaik - 尝试为您的组件提供默认的最小高度

于 2013-04-16T10:06:06.650 回答
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>
于 2017-11-25T07:40:43.673 回答
-1

你也在下面使用这个构造器,对吧?因为这是您在 xml 中创建视图时调用的方法。

public CustomLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
        // Do your stuff
}
于 2013-04-16T09:40:27.740 回答