When you need to extend Android native TextView, such as the need to give TextView a default color border of 10 pixels, when the width is set to wrap_content, height is not good processing. Most of the online said to set a default value, and then according to the measurement mode, take Measurespec.getsize (WIDTHMEASURESPEC) and the default value of the smaller values, I want to say that is nonsense. For example, I need a width of 200px, the default value is 50px, when the width is certainly not enough. First look at the following code
PackageCom.example.customview.view;ImportAndroid.content.Context;ImportAndroid.graphics.Canvas;ImportAndroid.graphics.Color;ImportAndroid.graphics.Paint;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;ImportAndroid.view.View.MeasureSpec;ImportAndroid.view.WindowManager;ImportAndroid.widget.TextView; Public classCustomtextviewextendsTextView {PrivatePaint Paint1; PrivatePaint Paint2; PublicCustomtextview (Context context) {Super(Context,NULL); } PublicCustomtextview (Context context, AttributeSet attrs) {Super(context, attrs); Initview (context, attrs); //TODO auto-generated Constructor stub } Private voidInitview (Context context, AttributeSet attrs) {paint1=NewPaint (); Paint1.setcolor (Getresources (). GetColor (Android. R.color.holo_blue_dark)); Paint1.setstyle (Paint.Style.FILL); Paint2=NewPaint (); Paint2.setstyle (Paint.Style.FILL); Paint2.setcolor (Color.green); } @Overrideprotected voidOnmeasure (intWidthmeasurespec,intHeightmeasurespec) { // //TODO auto-generated Method Stub Super. Onmeasure (Widthmeasurespec, Heightmeasurespec); intWidthmode =Measurespec.getmode (WIDTHMEASURESPEC); intWidthsize =measurespec.getsize (WIDTHMEASURESPEC); intHeightmode =Measurespec.getmode (HEIGHTMEASURESPEC); intHeightsize =measurespec.getsize (HEIGHTMEASURESPEC); if(Heightmode = = Measurespec.at_most&&widthmode = =measurespec.at_most) { //when the height width is wrap_contentSetmeasureddimension (Getmeasuredwidth () +20, Getmeasuredheight () +20); }} @Overrideprotected voidOnDraw (canvas canvas) {LOG.E ("Customtextview", "widthsize" + getmeasuredwidth () + "heightsize" +getmeasuredheight ()); Canvas.drawrect (0, 0, Getmeasuredwidth (), Getmeasuredwidth (), paint1); Canvas.drawrect (Ten, Getmeasuredwidth ()-10, Getmeasuredheight ()-10, Paint2); Canvas.translate (10, 10); Super. OnDraw (canvas); }}View Code
Attention:
if (Heightmode = = Measurespec.at_most&&widthmode = = Measurespec.at_most ) {// Height width is wrap_content setmeasureddimension (Getmeasuredwidth () +20, Getmeasuredheight () +20); }
By calling TextView's Getmeasuredwidth (), the Getmeasuredheight () method gets the width height without the bounding rectangle, which is achieved by adding the appropriate offset.
Use the following:
<LinearLayoutxmlns: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.CustomTextViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Number of participants"/></LinearLayout>
The results of the operation are as follows:
Android inherits TextView Height-width calculation problem