Android的滚动型布局(WRAP_CONTENT,最大尺寸) [英] android ScrollView layout (wrap_content, maximum size)

查看:195
本文介绍了Android的滚动型布局(WRAP_CONTENT,最大尺寸)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我想我的滚动型的样子:

Here is what I would like my ScrollView to look like:


  • 的最大尺寸与layout_weight定义(这样滚动型低于其他项目可以正常显示)

  • 如果含量比最大尺寸小,那么它只是表现为与 layout_height =WRAP_CONTENT

  • The maximum size is defined with the layout_weight (so that other items below the ScrollView can be displayed properly)
  • If the content is smaller than that maximum size, then it just behaves as with layout_height="wrap_content"

下面是我目前有:

<ScrollView
            android:layout_height="wrap_content"
            android:layout_width="fill_parent"
            android:measureAllChildren="true"
            android:fillViewport="false"
            >

我不认为 measureAllChildren 确实什么都...

如果我想补充的android:layout_weight ,大小将永远是我想什么,最大的是。没有它,它只是扩展了超过它应该...

If I add android:layout_weight, the size will always be what I would like the maximum to be. Without it, it just extends more than it should...

我不介意延长滚动型类来更改onMeasure的行为,如果我需要...?

I don't mind extending the ScrollView class to change the behavior of onMeasure if I need to...?

PS:如果使一个差异,我想从这个Froyo的工作向前

PS: If that makes a differences, I am trying to get this working from Froyo onward.

推荐答案

我结束了写我自己的类,延长滚动型

I ended up writing my own class, extending ScrollView

既然你问...这里是code。也许不是最干净的,但我想要做什么。

Since you ask...here is the code. Probably not the cleanest but it does what I want.

请注意,它期望在创建视图时layout_weight进行设置,你不应该设置weigthSum在父的LinearLayout或者你会得到有趣的事情(因为这一块的重量从原来的值更改为0视对滚动型的内容的大小)

Note that it expects the layout_weight to be set when the view is created and you should not set the weigthSum in the parent LinearLayout or you'll get funny things (since the weight of this one changes from the original value to 0 depending on the size of the content of the ScrollView)

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

First, in the layout file, the view is declared like this:

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

然后code小部件:

Then the code for the widget:

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

这篇关于Android的滚动型布局(WRAP_CONTENT,最大尺寸)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆