android ScrollView布局(wrap_content,最大大小)

Mat*_*ieu 4 layout android scrollview

这是我希望我的ScrollView看起来像:

  • 使用layout_weight定义最大大小(以便可以正确显示ScrollView下方的其他项目)
  • 如果内容小于该最大大小,那么它只是表现如下 layout_height="wrap_content"

这是我目前拥有的:

<ScrollView
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:measureAllChildren="true"
            android:fillViewport="false"
            >
Run Code Online (Sandbox Code Playgroud)

我认为measureAllChildren真的没有任何作用......

如果我添加android:layout_weight,大小将始终是我想要的最大值.没有它,它只会延伸超过应有的...

我不介意扩展ScrollView类来改变onMeasure的行为,如果我需要......?

PS:如果这有所不同,我试图让这个从Froyo开始工作.

Mat*_*ieu 7

我最后编写了自己的类,扩展了ScrollView

既然你问......这是代码.可能不是最干净但它做我想要的.

请注意,它期望在创建视图时设置layout_weight,并且不应在父LinearLayout中设置weigthSum,否则你会得到有趣的东西(因为这个的权重从原始值变为0,具体取决于大小ScrollView的内容)

首先,在布局文件中,视图声明如下:

<com.matthieu.widget.ShrinkingScrollView
    android:id="@+id/scroll"
    android:scrollbars="vertical"
    android:layout_height="0dp"
    android:layout_width="fill_parent"
    android:layout_weight="4"
    android:background="#cc0000"
    >
    <TextView
        android:id="@+id/in_scroll_view"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:background="#0000bb"
        />
</com.matthieu.widget.ShrinkingScrollView>
Run Code Online (Sandbox Code Playgroud)

然后是小部件的代码:

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class ShrinkingScrollView extends ScrollView {
    private float original_weight=-1;
    public ShrinkingScrollView(Context context) {
        super(context);
    }

    public ShrinkingScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ShrinkingScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) getLayoutParams();
        float previous_weight = params.weight;

        if (original_weight == -1)
            original_weight = params.weight;

        if ((getChildCount()>0) && (getVisibility()!=GONE)) {
            super.onMeasure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));
            int overall_height = getChildAt(0).getMeasuredHeight();
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            if (getMeasuredHeight() >= overall_height) {
                if (previous_weight != 0) {
                    params.weight=0;
                    params.height = overall_height;
                    setLayoutParams(params);
                    post(new Runnable() {
                        public void run() {
                            requestLayout();
                        }
                    });
                }

                setMeasuredDimension(getMeasuredWidth(),overall_height);
            }
            else if (previous_weight == 0) {
                params.weight = original_weight;
                params.height = 0;
                setLayoutParams(params);
                post(new Runnable() {
                    public void run() {
                        requestLayout();
                    }
                });
            }
        }
        else {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)