How to set a max height of a any kind of view?

泄露秘密 提交于 2020-01-16 19:18:53

问题


Background

I'm trying to put a view within a container view, so that the container will allow the view to have any height (and/or width,depending on your specification) , yet not pass a specific one.

The problem

There is no way to define "maxHeight" for any kind of view. You can only use exact size , use some tricks to take the spare size (layout_weight) or make the scrollView take as much space as it needs.

The only similar thing is for EditText , which you can use "maxLines" together with "setMovementMethod", as shown here .

What I've tried

I've tried to make the next viewGroup that contains the scrollView, but it didn't work as expected:

public class SizeLimitLayout extends FrameLayout {
    private int mMaxHeightInPixels = -1;
    private int mMaxWidthInPixels = -1;

    public SizeLimitLayout(final Context context) {
        super(context);
    }

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

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

    public void setMaxHeight(final int maxHeightInPixels) {
        this.mMaxHeightInPixels = maxHeightInPixels;
    }

    public void setMaxWidth(final int maxWidthInPixels) {
        this.mMaxWidthInPixels = maxWidthInPixels;
    }

    @Override
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
        // time to decide what is the size of this view
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = getMeasuredWidth(), height = getMeasuredHeight();
        if (mMaxWidthInPixels >= 0)
            width = Math.min(mMaxWidthInPixels, width);
        if (mMaxHeightInPixels >= 0)
            height = Math.min(mMaxHeightInPixels, height);
        setMeasuredDimension(width, height);
    }
}

Here's a sample of how I tried to use what I made:

MainActivity.java

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final SizeLimitLayout sizeLimitLayout = (SizeLimitLayout) findViewById(R.id.sizeLimitLayout);
        sizeLimitLayout.setMaxHeight((int) convertDpToPixels(this, 160));
        final View btn = findViewById(R.id.button);
        final ViewGroup container = (ViewGroup) findViewById(R.id.container);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
                final ImageView iv = new ImageView(MainActivity.this);
                iv.setImageResource(R.drawable.ic_launcher);
                container.addView(iv);
            }
        });
    }

    public static float convertDpToPixels(final Context context, final float dp) {
        final float pixels = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources()
                .getDisplayMetrics());
        return pixels;
    }
}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.viewsizelimittest.MainActivity"
    tools:ignore="MergeRootFrame" >

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="click"
        tools:ignore="HardcodedText" />

    <com.example.viewsizelimittest.SizeLimitLayout
        android:id="@+id/sizeLimitLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <ScrollView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#33ff0000"
            android:fillViewport="true" >

            <LinearLayout
                android:id="@+id/container"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >
            </LinearLayout>
        </ScrollView>
    </com.example.viewsizelimittest.SizeLimitLayout>

</LinearLayout>

The question

The code almost works, but the container doesn't allow to scroll .

What can I do to fix it?


回答1:


There isn't build it row limiter but in this question there are some solutions that might help to solve your problem EditText maxLines not working - user can still input more lines than set



来源:https://stackoverflow.com/questions/22984740/how-to-set-a-max-height-of-a-any-kind-of-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!