10
10
import android .support .annotation .Nullable ;
11
11
import android .support .annotation .Px ;
12
12
import android .support .v7 .widget .AppCompatTextView ;
13
+ import android .text .TextUtils ;
13
14
import android .util .AttributeSet ;
14
15
import android .view .Gravity ;
15
16
16
-
17
17
import java .lang .annotation .Retention ;
18
18
import java .lang .annotation .RetentionPolicy ;
19
19
import java .util .Arrays ;
@@ -134,16 +134,19 @@ protected void onDraw(Canvas canvas) {
134
134
135
135
if (enableCenterDrawables && (isCenterHorizontal | isCenterVertical )) {
136
136
137
+ //有文字就才位移画布了
138
+ boolean textNoEmpty = !TextUtils .isEmpty (getText ());
137
139
//画布的偏移量
138
140
int transX = 0 , transY = 0 ;
139
141
142
+
140
143
if (mDrawables [POSITION .START ] != null ) {
141
144
Rect bounds = mDrawablesBounds [POSITION .START ];
142
145
int offset = (int ) calcOffset (POSITION .START );
143
146
mDrawables [POSITION .START ].setBounds (bounds .left + offset , bounds .top ,
144
147
bounds .right + offset , bounds .bottom );
145
148
146
- if (isCenterHorizontal )
149
+ if (isCenterHorizontal && textNoEmpty )
147
150
transX -= (mDrawablesBounds [POSITION .START ].width () + getCompoundDrawablePadding ()) >> 1 ;
148
151
}
149
152
@@ -154,7 +157,7 @@ protected void onDraw(Canvas canvas) {
154
157
mDrawables [POSITION .TOP ].setBounds (bounds .left , bounds .top + offset ,
155
158
bounds .right , bounds .bottom + offset );
156
159
157
- if (isCenterVertical )
160
+ if (isCenterVertical && textNoEmpty )
158
161
transY -= (mDrawablesBounds [POSITION .TOP ].height () + getCompoundDrawablePadding ()) >> 1 ;
159
162
}
160
163
@@ -164,7 +167,7 @@ protected void onDraw(Canvas canvas) {
164
167
mDrawables [POSITION .END ].setBounds (bounds .left + offset , bounds .top ,
165
168
bounds .right + offset , bounds .bottom );
166
169
167
- if (isCenterHorizontal )
170
+ if (isCenterHorizontal && textNoEmpty )
168
171
transX += (mDrawablesBounds [POSITION .END ].width () + getCompoundDrawablePadding ()) >> 1 ;
169
172
}
170
173
@@ -174,11 +177,12 @@ protected void onDraw(Canvas canvas) {
174
177
mDrawables [POSITION .BOTTOM ].setBounds (bounds .left , bounds .top + offset ,
175
178
bounds .right , bounds .bottom + offset );
176
179
177
- if (isCenterVertical )
180
+ if (isCenterVertical && textNoEmpty )
178
181
transY += (mDrawablesBounds [POSITION .BOTTOM ].height () + getCompoundDrawablePadding ()) >> 1 ;
179
182
}
180
183
181
- if (enableTextInCenter ) {
184
+
185
+ if (enableTextInCenter && textNoEmpty ) {
182
186
canvas .translate (transX , transY );
183
187
this .canvasTransX = transX ;
184
188
this .canvasTransY = transY ;
0 commit comments