【问题标题】:ScrollView makes a custom layout invisibleScrollView 使自定义布局不可见
【发布时间】:2013-04-16 09:26:42
【问题描述】:

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

【问题讨论】:

    标签: android layout viewgroup


    【解决方案1】:

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

    现在对我有用。

    【讨论】:

      【解决方案2】:
      <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

      【讨论】:

        【解决方案3】:

        您使用wrap_content 表示高度:

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

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

        【讨论】:

        • 完成了,我还是什么都没看到
        • 您的自定义布局是否在绘制任何东西?尝试在你的视图上设置 android:background="@android:color/black" - 这会给你一个黑盒子吗?
        • 是的,有滚动视图,如果我放那个背景,它就会变成全黑。
        • 您可以将背景添加到您的 CustomLayout(不是 ScrollView)吗?
        • 嗯...我试过了,没有什么是黑色的...看起来它没有画出来
        【解决方案4】:

        检查这是否完美!

         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;
            }
        
        
        }
        

        在 values 文件夹中创建 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>
        

        【讨论】:

          【解决方案5】:

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

          public CustomLayout(Context context, AttributeSet attrs) {
              super(context, attrs);
                  // Do your stuff
          }
          

          【讨论】:

          • 是的,我的 CustomView 中有那个构造函数
          • 您介意发布您的 CustomView 课程吗,目前很难说出了什么问题。
          猜你喜欢
          • 2023-03-09
          • 1970-01-01
          • 1970-01-01
          • 2015-10-20
          • 1970-01-01
          • 2018-11-05
          • 1970-01-01
          • 2016-02-04
          • 1970-01-01
          相关资源
          最近更新 更多