簡體   English   中英

如何設置任何視圖的最大高度?

[英]How to set a max height of a any kind of view?

背景

我試圖將一個視圖放入一個容器視圖中,以便該容器將允許該視圖具有任何高度(和/或寬度,取決於您的規范),但不傳遞特定的視圖。

問題

無法為任何類型的視圖定義“ maxHeight”。 您只能使用精確大小,使用一些技巧來獲取備用大小(layout_weight)或使scrollView占用所需的空間。

唯一的類似的事情是對的EditText,您可以使用“MAXLINES”連同“setMovementMethod”,如圖所示這里

我嘗試過的

我試圖制作包含scrollView的下一個viewGroup,但是它沒有按預期工作:

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

以下是我嘗試使用自己制作的內容的示例:

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>

問題

該代碼幾乎可以工作,但是容器不允許滾動。

我該如何解決?

沒有構建行限制器,但是在此問題中,有些解決方案可能有助於解決您的問題EditText maxLines不起作用-用戶仍然可以輸入比設置更多的行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM