android繼承TextView的高度寬度計算問題

來源:互聯網
上載者:User

標籤:

      當需要擴充android原生TextView的時候,比如需要給TextView預設加上10像素的顏色邊框時,當設定寬高為wrap_content時,高度並不好處理。網上大部分人云亦云的說設定一個預設值,然後根據測量模式,取 MeasureSpec.getSize(widthMeasureSpec)和預設值中的較小值,我想說就是扯淡。比如說我需要的寬度是200px,預設值是50px,此時寬度肯定不夠。先看如下代碼

package com.example.customview.view;import android.content.Context;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.util.Log;import android.view.View.MeasureSpec;import android.view.WindowManager;import android.widget.TextView;public class CustomTextView extends TextView {    private Paint paint1;    private Paint paint2;    public CustomTextView(Context context) {        super(context, null);    }    public CustomTextView(Context context, AttributeSet attrs) {        super(context, attrs);        initView(context, attrs);        // TODO Auto-generated constructor stub    }    private void initView(Context context, AttributeSet attrs) {        paint1 = new Paint();        paint1.setColor(getResources().getColor(android.R.color.holo_blue_dark));        paint1.setStyle(Paint.Style.FILL);        paint2 = new Paint();        paint2.setStyle(Paint.Style.FILL);        paint2.setColor(Color.GREEN);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        // // TODO Auto-generated method stub        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        int widthMode = MeasureSpec.getMode(widthMeasureSpec);        int widthSize = MeasureSpec.getSize(widthMeasureSpec);        int heightMode = MeasureSpec.getMode(heightMeasureSpec);        int heightSize = MeasureSpec.getSize(heightMeasureSpec);        if(heightMode == MeasureSpec.AT_MOST&&widthMode == MeasureSpec.AT_MOST){            //高度寬度為wrap_content時            setMeasuredDimension(getMeasuredWidth()+20, getMeasuredHeight()+20);        }    }    @Override    protected void onDraw(Canvas canvas) {        Log.e("CustomTextView", "widthSize" + getMeasuredWidth() + "heightSize"                + getMeasuredHeight());        canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredWidth(), paint1);        canvas.drawRect(10, 10, getMeasuredWidth() - 10,                getMeasuredHeight() - 10, paint2);        canvas.translate(10, 10);        super.onDraw(canvas);    }}
View Code

注意:

  

if(heightMode == MeasureSpec.AT_MOST&&widthMode == MeasureSpec.AT_MOST){            //高度寬度為wrap_content時            setMeasuredDimension(getMeasuredWidth()+20, getMeasuredHeight()+20);        }

 

通過調用TextView的getMeasuredWidth(),getMeasuredHeight()的方法擷取擷取不加邊框時的寬高,在加上適當的位移量,就能實現了。

用法如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:app="http://schemas.android.com/apk/res-auto"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:gravity="center"    android:orientation="vertical" >    <com.example.customview.view.CustomTextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="參與人數"/></LinearLayout>

運行結果如下:

android繼承TextView的高度寬度計算問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.