55import android .graphics .Canvas ;
66import android .graphics .Rect ;
77import android .graphics .drawable .Drawable ;
8+ import android .text .TextUtils ;
89import android .util .AttributeSet ;
910import android .view .Gravity ;
1011
@@ -126,16 +127,19 @@ protected void onDraw(Canvas canvas) {
126127
127128 if (enableCenterDrawables && (isCenterHorizontal | isCenterVertical )) {
128129
130+ //有文字就才位移画布了
131+ boolean textNoEmpty = !TextUtils .isEmpty (getText ());
129132 //画布的偏移量
130133 int transX = 0 , transY = 0 ;
131134
135+
132136 if (mDrawables [POSITION .START ] != null ) {
133137 Rect bounds = mDrawablesBounds [POSITION .START ];
134138 int offset = (int ) calcOffset (POSITION .START );
135139 mDrawables [POSITION .START ].setBounds (bounds .left + offset , bounds .top ,
136140 bounds .right + offset , bounds .bottom );
137141
138- if (isCenterHorizontal )
142+ if (isCenterHorizontal && textNoEmpty )
139143 transX -= (mDrawablesBounds [POSITION .START ].width () + getCompoundDrawablePadding ()) >> 1 ;
140144 }
141145
@@ -146,7 +150,7 @@ protected void onDraw(Canvas canvas) {
146150 mDrawables [POSITION .TOP ].setBounds (bounds .left , bounds .top + offset ,
147151 bounds .right , bounds .bottom + offset );
148152
149- if (isCenterVertical )
153+ if (isCenterVertical && textNoEmpty )
150154 transY -= (mDrawablesBounds [POSITION .TOP ].height () + getCompoundDrawablePadding ()) >> 1 ;
151155 }
152156
@@ -156,7 +160,7 @@ protected void onDraw(Canvas canvas) {
156160 mDrawables [POSITION .END ].setBounds (bounds .left + offset , bounds .top ,
157161 bounds .right + offset , bounds .bottom );
158162
159- if (isCenterHorizontal )
163+ if (isCenterHorizontal && textNoEmpty )
160164 transX += (mDrawablesBounds [POSITION .END ].width () + getCompoundDrawablePadding ()) >> 1 ;
161165 }
162166
@@ -166,11 +170,12 @@ protected void onDraw(Canvas canvas) {
166170 mDrawables [POSITION .BOTTOM ].setBounds (bounds .left , bounds .top + offset ,
167171 bounds .right , bounds .bottom + offset );
168172
169- if (isCenterVertical )
173+ if (isCenterVertical && textNoEmpty )
170174 transY += (mDrawablesBounds [POSITION .BOTTOM ].height () + getCompoundDrawablePadding ()) >> 1 ;
171175 }
172176
173- if (enableTextInCenter ) {
177+
178+ if (enableTextInCenter && textNoEmpty ) {
174179 canvas .translate (transX , transY );
175180 this .canvasTransX = transX ;
176181 this .canvasTransY = transY ;
0 commit comments