【问题标题】:What is the difference between setMinHeight and setMinimumHeight on a View in Android?Android中视图上的setMinHeight和setMinimumHeight有什么区别?
【发布时间】:2015-06-24 09:37:49
【问题描述】:

我有一个自定义 View,其中包含 8 个 TextViews,我将其用作表格中的一行。插入数据后,我需要设置这些孩子的最小高度,因为我希望所有 8 个TextViews 的高度扩展到最高的高度。

我目前的代码如下:

for(int i = 0; i < m_textViews.length; i++)
{
    m_textViews[i].setMinHeight(heightPx);
    m_textViews[i].setHeight(heightPx);
}

我正在尝试提高代码性能,这让我想知道setMinHeight()setMinimumHeight() 之间究竟有什么区别?

提前致谢

【问题讨论】:

    标签: android android-layout layout android-custom-view


    【解决方案1】:

    我建议使用 setMinHeight,因为它是专门为 TextViews 编写的,它会更新 mminMode 以保存 PIXELS 值

    这是来自 TextView.java 源代码的 SetMinHeight

    /**
    * Makes the TextView at least this many pixels tall.
    *
    * Setting this value overrides any other (minimum) number of lines setting.
    *
    * @attr ref android.R.styleable#TextView_minHeight
    */
    @android.view.RemotableViewMethod
    public void setMinHeight(int minHeight) {
        mMinimum = minHeight;
        mMinMode = PIXELS;
        requestLayout();
        invalidate();
    }
    

    这是来自 View.java 源代码的 SetMinimumHeight

    /**
    * Sets the minimum height of the view. It is not guaranteed the view will
    * be able to achieve this minimum height (for example, if its parent layout
    * constrains it with less available height).
    *
    * @param minHeight The minimum height the view will try to be.
    *
    * @see #getMinimumHeight()
    *
    * @attr ref android.R.styleable#View_minHeight
    */
    public void setMinimumHeight(int minHeight) {
        mMinHeight = minHeight 
        requestLayout();
    }
    

    参考资料:

    TextView.java:

    http://androidxref.com/5.1.0_r1/xref/frameworks/base/core/java/android/widget/TextView.java

    View.java:

    http://androidxref.com/5.1.0_r1/xref/frameworks/base/core/java/android/view/View.java

    【讨论】:

      【解决方案2】:

      setMinHeight(int minHeight)

      使 TextView 至少有这么多像素高。设置此值会覆盖任何其他(最小)行数设置。

      setMinimumHeight(int minHeight)

      设置视图的最小高度。不能保证视图能够达到这个最小高度(例如,如果它的父布局限制它具有较少的可用高度)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-01-17
        • 2010-09-10
        • 2018-06-07
        • 2012-07-31
        • 1970-01-01
        • 1970-01-01
        • 2023-01-18
        相关资源
        最近更新 更多