1010import android .support .annotation .Nullable ;
1111import android .support .annotation .Px ;
1212import android .support .v7 .widget .AppCompatTextView ;
13+ import android .text .TextUtils ;
1314import android .util .AttributeSet ;
1415import android .view .Gravity ;
1516
16-
1717import java .lang .annotation .Retention ;
1818import java .lang .annotation .RetentionPolicy ;
1919import java .util .Arrays ;
@@ -134,16 +134,19 @@ protected void onDraw(Canvas canvas) {
134134
135135 if (enableCenterDrawables && (isCenterHorizontal | isCenterVertical )) {
136136
137+ //有文字就才位移画布了
138+ boolean textNoEmpty = !TextUtils .isEmpty (getText ());
137139 //画布的偏移量
138140 int transX = 0 , transY = 0 ;
139141
142+
140143 if (mDrawables [POSITION .START ] != null ) {
141144 Rect bounds = mDrawablesBounds [POSITION .START ];
142145 int offset = (int ) calcOffset (POSITION .START );
143146 mDrawables [POSITION .START ].setBounds (bounds .left + offset , bounds .top ,
144147 bounds .right + offset , bounds .bottom );
145148
146- if (isCenterHorizontal )
149+ if (isCenterHorizontal && textNoEmpty )
147150 transX -= (mDrawablesBounds [POSITION .START ].width () + getCompoundDrawablePadding ()) >> 1 ;
148151 }
149152
@@ -154,7 +157,7 @@ protected void onDraw(Canvas canvas) {
154157 mDrawables [POSITION .TOP ].setBounds (bounds .left , bounds .top + offset ,
155158 bounds .right , bounds .bottom + offset );
156159
157- if (isCenterVertical )
160+ if (isCenterVertical && textNoEmpty )
158161 transY -= (mDrawablesBounds [POSITION .TOP ].height () + getCompoundDrawablePadding ()) >> 1 ;
159162 }
160163
@@ -164,7 +167,7 @@ protected void onDraw(Canvas canvas) {
164167 mDrawables [POSITION .END ].setBounds (bounds .left + offset , bounds .top ,
165168 bounds .right + offset , bounds .bottom );
166169
167- if (isCenterHorizontal )
170+ if (isCenterHorizontal && textNoEmpty )
168171 transX += (mDrawablesBounds [POSITION .END ].width () + getCompoundDrawablePadding ()) >> 1 ;
169172 }
170173
@@ -174,11 +177,12 @@ protected void onDraw(Canvas canvas) {
174177 mDrawables [POSITION .BOTTOM ].setBounds (bounds .left , bounds .top + offset ,
175178 bounds .right , bounds .bottom + offset );
176179
177- if (isCenterVertical )
180+ if (isCenterVertical && textNoEmpty )
178181 transY += (mDrawablesBounds [POSITION .BOTTOM ].height () + getCompoundDrawablePadding ()) >> 1 ;
179182 }
180183
181- if (enableTextInCenter ) {
184+
185+ if (enableTextInCenter && textNoEmpty ) {
182186 canvas .translate (transX , transY );
183187 this .canvasTransX = transX ;
184188 this .canvasTransY = transY ;
0 commit comments