天天看点

getMeasuredHeight()跟getHeight()区别

记一次在onMeasure方法中getMeasuredHeight()跟getHeight()区别

布局
<com.kaytrip.trip.expandabletextview.ExpandableTextView
        android:background="@color/colorAccent"
        xmlns:expandableTextView="http://schemas.android.com/apk/res-auto"
        android:id="@+id/expand_text_view"
        expandableTextView:maxCollapsedLines="3"
        expandableTextView:animDuration="200"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/expandable_text"
            android:lineSpacingMultiplier="1.2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:textColor="#000"
            android:textSize="40px"
            android:layout_marginRight="48px"
            android:layout_marginLeft="48px" />
        <ImageButton
            android:id="@id/expand_collapse"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="0dp"
            android:layout_marginRight="5px"
            android:layout_gravity="right"
            android:background="@android:color/transparent"/>
    </com.kaytrip.trip.expandabletextview.ExpandableTextView>
           
测量ExpandableTextView的高度
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //id为expandable_text的TextView
        mTv.setLines();
            this.post(new Runnable() {
            @Override
            public void run() {
                mMarginBetweenTxtAndBottom = getHeight() - mTv.getHeight();
                Log.e("onMeasure", "getHeight(): "+getHeight()+"//mTv.getHeight()"+mTv.getHeight()+"" );
            }
        });
        //Log.e("onMeasure", "getHeight(): "+mTv.getHeight()+"" );
        Log.e("onMeasure", "getMeasuredHeight(): "+getMeasuredHeight()+"" );

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
           

通过以上的log发现getMeasuredHeight()是ExpandableTextView的实际高度,而getHeight()是显示的高度,因为里面的TestView即mTv.setLines(3) 所以导致比实际的高度要小。

继续阅读