天天看點

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) 是以導緻比實際的高度要小。

繼續閱讀