Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – ScrollView使自定义布局不可见大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我制作了一个自定义viewgroup,我需要在我的应用程序中使用它,但我需要将它放在ScrollView中.当只使用我的自定义viewGroup进行布局时,一切正常,但是当我把它放在ScrollView中时,我看不到任何东西.
我的布局:
<ScrollView xmlns:android="http://scheR_577_11845@as.android.com/apk/res/android"
    xmlns:tools="http://scheR_577_11845@as.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));

现在对我来说它有效.

大佬总结

以上是大佬教程为你收集整理的android – ScrollView使自定义布局不可见全部内容,希望文章能够帮你解决android – ScrollView使自定义布局不可见所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。