@@ -129,8 +129,10 @@ private void initAttributeSet(Context context, AttributeSet attrs) {
129129 }
130130 TypedArray a = context .obtainStyledAttributes (attrs , R .styleable .RTextView );
131131 //icon
132+ Drawable drawableLeft , drawableRight , drawableTop , drawableBottom , drawableStart , drawableEnd ;
132133 //Vector兼容处理
133134 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
135+ //自定义属性
134136 mIconNormalLeft = a .getDrawable (R .styleable .RTextView_icon_normal_left );
135137 mIconPressedLeft = a .getDrawable (R .styleable .RTextView_icon_pressed_left );
136138 mIconUnableLeft = a .getDrawable (R .styleable .RTextView_icon_unable_left );
@@ -151,90 +153,54 @@ private void initAttributeSet(Context context, AttributeSet attrs) {
151153 mIconUnableBottom = a .getDrawable (R .styleable .RTextView_icon_unable_bottom );
152154 mIconSelectedBottom = a .getDrawable (R .styleable .RTextView_icon_selected_bottom );
153155 mIconCheckedBottom = a .getDrawable (R .styleable .RTextView_icon_checked_bottom );
156+ //兼容系统原生drawableLeft
157+ drawableLeft = a .getDrawable (R .styleable .RTextView_android_drawableLeft );
158+ drawableRight = a .getDrawable (R .styleable .RTextView_android_drawableRight );
159+ drawableTop = a .getDrawable (R .styleable .RTextView_android_drawableTop );
160+ drawableBottom = a .getDrawable (R .styleable .RTextView_android_drawableBottom );
161+ drawableStart = a .getDrawable (R .styleable .RTextView_android_drawableStart );
162+ drawableEnd = a .getDrawable (R .styleable .RTextView_android_drawableEnd );
154163 //版本兼容代码
155164 mIconNormal = a .getDrawable (R .styleable .RTextView_icon_src_normal );
156165 mIconPressed = a .getDrawable (R .styleable .RTextView_icon_src_pressed );
157166 mIconUnable = a .getDrawable (R .styleable .RTextView_icon_src_unable );
158167 mIconSelected = a .getDrawable (R .styleable .RTextView_icon_src_selected );
159168 mIconChecked = a .getDrawable (R .styleable .RTextView_icon_src_checked );
160-
161169 } else {
162- int normalIdLeft = a .getResourceId (R .styleable .RTextView_icon_normal_left , -1 );
163- int pressedIdLeft = a .getResourceId (R .styleable .RTextView_icon_pressed_left , -1 );
164- int unableIdLeft = a .getResourceId (R .styleable .RTextView_icon_unable_left , -1 );
165- int selectedIdLeft = a .getResourceId (R .styleable .RTextView_icon_selected_left , -1 );
166- int checkedIdLeft = a .getResourceId (R .styleable .RTextView_icon_checked_left , -1 );
167- int normalIdRight = a .getResourceId (R .styleable .RTextView_icon_normal_right , -1 );
168- int pressedIdRight = a .getResourceId (R .styleable .RTextView_icon_pressed_right , -1 );
169- int unableIdRight = a .getResourceId (R .styleable .RTextView_icon_unable_right , -1 );
170- int selectedIdRight = a .getResourceId (R .styleable .RTextView_icon_selected_right , -1 );
171- int checkedIdRight = a .getResourceId (R .styleable .RTextView_icon_checked_right , -1 );
172- int normalIdTop = a .getResourceId (R .styleable .RTextView_icon_normal_top , -1 );
173- int pressedIdTop = a .getResourceId (R .styleable .RTextView_icon_pressed_top , -1 );
174- int unableIdTop = a .getResourceId (R .styleable .RTextView_icon_unable_top , -1 );
175- int selectedIdTop = a .getResourceId (R .styleable .RTextView_icon_selected_top , -1 );
176- int checkedIdTop = a .getResourceId (R .styleable .RTextView_icon_checked_top , -1 );
177- int normalIdBottom = a .getResourceId (R .styleable .RTextView_icon_normal_bottom , -1 );
178- int pressedIdBottom = a .getResourceId (R .styleable .RTextView_icon_pressed_bottom , -1 );
179- int unableIdBottom = a .getResourceId (R .styleable .RTextView_icon_unable_bottom , -1 );
180- int selectedIdBottom = a .getResourceId (R .styleable .RTextView_icon_selected_bottom , -1 );
181- int checkedIdBottom = a .getResourceId (R .styleable .RTextView_icon_checked_bottom , -1 );
182- if (normalIdLeft != -1 )
183- mIconNormalLeft = AppCompatResources .getDrawable (context , normalIdLeft );
184- if (pressedIdLeft != -1 )
185- mIconPressedLeft = AppCompatResources .getDrawable (context , pressedIdLeft );
186- if (unableIdLeft != -1 )
187- mIconUnableLeft = AppCompatResources .getDrawable (context , unableIdLeft );
188- if (selectedIdLeft != -1 )
189- mIconSelectedLeft = AppCompatResources .getDrawable (context , selectedIdLeft );
190- if (checkedIdLeft != -1 )
191- mIconCheckedLeft = AppCompatResources .getDrawable (context , checkedIdLeft );
192- if (normalIdRight != -1 )
193- mIconNormalRight = AppCompatResources .getDrawable (context , normalIdRight );
194- if (pressedIdRight != -1 )
195- mIconPressedRight = AppCompatResources .getDrawable (context , pressedIdRight );
196- if (unableIdRight != -1 )
197- mIconUnableRight = AppCompatResources .getDrawable (context , unableIdRight );
198- if (selectedIdRight != -1 )
199- mIconSelectedRight = AppCompatResources .getDrawable (context , selectedIdRight );
200- if (checkedIdRight != -1 )
201- mIconCheckedRight = AppCompatResources .getDrawable (context , checkedIdRight );
202- if (normalIdTop != -1 )
203- mIconNormalTop = AppCompatResources .getDrawable (context , normalIdTop );
204- if (pressedIdTop != -1 )
205- mIconPressedTop = AppCompatResources .getDrawable (context , pressedIdTop );
206- if (unableIdTop != -1 )
207- mIconUnableTop = AppCompatResources .getDrawable (context , unableIdTop );
208- if (selectedIdTop != -1 )
209- mIconSelectedTop = AppCompatResources .getDrawable (context , selectedIdTop );
210- if (checkedIdTop != -1 )
211- mIconCheckedTop = AppCompatResources .getDrawable (context , checkedIdTop );
212- if (normalIdBottom != -1 )
213- mIconNormalBottom = AppCompatResources .getDrawable (context , normalIdBottom );
214- if (pressedIdBottom != -1 )
215- mIconPressedBottom = AppCompatResources .getDrawable (context , pressedIdBottom );
216- if (unableIdBottom != -1 )
217- mIconUnableBottom = AppCompatResources .getDrawable (context , unableIdBottom );
218- if (selectedIdBottom != -1 )
219- mIconSelectedBottom = AppCompatResources .getDrawable (context , selectedIdBottom );
220- if (checkedIdBottom != -1 )
221- mIconCheckedBottom = AppCompatResources .getDrawable (context , checkedIdBottom );
170+ //自定义属性
171+ mIconNormalLeft = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_normal_left , -1 ));
172+ mIconPressedLeft = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_pressed_left , -1 ));
173+ mIconUnableLeft = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_unable_left , -1 ));
174+ mIconSelectedLeft = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_selected_left , -1 ));
175+ mIconCheckedLeft = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_checked_left , -1 ));
176+ mIconNormalRight = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_normal_right , -1 ));
177+ mIconPressedRight = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_pressed_right , -1 ));
178+ mIconUnableRight = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_unable_right , -1 ));
179+ mIconSelectedRight = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_selected_right , -1 ));
180+ mIconCheckedRight = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_checked_right , -1 ));
181+ mIconNormalTop = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_normal_top , -1 ));
182+ mIconPressedTop = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_pressed_top , -1 ));
183+ mIconUnableTop = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_unable_top , -1 ));
184+ mIconSelectedTop = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_selected_top , -1 ));
185+ mIconCheckedTop = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_checked_top , -1 ));
186+ mIconNormalBottom = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_normal_bottom , -1 ));
187+ mIconPressedBottom = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_pressed_bottom , -1 ));
188+ mIconUnableBottom = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_unable_bottom , -1 ));
189+ mIconSelectedBottom = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_selected_bottom , -1 ));
190+ mIconCheckedBottom = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_checked_bottom , -1 ));
191+ //兼容系统原生drawableLeft
192+ drawableLeft = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_android_drawableLeft , -1 ));
193+ drawableRight = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_android_drawableRight , -1 ));
194+ drawableTop = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_android_drawableTop , -1 ));
195+ drawableBottom = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_android_drawableBottom , -1 ));
196+ drawableStart = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_android_drawableStart , -1 ));
197+ drawableEnd = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_android_drawableEnd , -1 ));
222198 //版本兼容代码
223- int normalId = a .getResourceId (R .styleable .RTextView_icon_src_normal , -1 );
224- int pressedId = a .getResourceId (R .styleable .RTextView_icon_src_pressed , -1 );
225- int unableId = a .getResourceId (R .styleable .RTextView_icon_src_unable , -1 );
226- int selectedId = a .getResourceId (R .styleable .RTextView_icon_src_selected , -1 );
227- int checkedId = a .getResourceId (R .styleable .RTextView_icon_src_checked , -1 );
228- if (normalId != -1 )
229- mIconNormal = AppCompatResources .getDrawable (context , normalId );
230- if (pressedId != -1 )
231- mIconPressed = AppCompatResources .getDrawable (context , pressedId );
232- if (unableId != -1 )
233- mIconUnable = AppCompatResources .getDrawable (context , unableId );
234- if (selectedId != -1 )
235- mIconSelected = AppCompatResources .getDrawable (context , selectedId );
236- if (checkedId != -1 )
237- mIconChecked = AppCompatResources .getDrawable (context , checkedId );
199+ mIconNormal = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_src_normal , -1 ));
200+ mIconPressed = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_src_pressed , -1 ));
201+ mIconUnable = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_src_unable , -1 ));
202+ mIconSelected = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_src_selected , -1 ));
203+ mIconChecked = AppCompatResources .getDrawable (context , a .getResourceId (R .styleable .RTextView_icon_src_checked , -1 ));
238204 }
239205 mIconWidthLeft = a .getDimensionPixelSize (R .styleable .RTextView_icon_width_left , 0 );
240206 mIconHeightLeft = a .getDimensionPixelSize (R .styleable .RTextView_icon_height_left , 0 );
@@ -248,34 +214,18 @@ private void initAttributeSet(Context context, AttributeSet attrs) {
248214 mIconWidth = a .getDimensionPixelSize (R .styleable .RTextView_icon_width , 0 );
249215 mIconHeight = a .getDimensionPixelSize (R .styleable .RTextView_icon_height , 0 );
250216 mIconDirection = a .getInt (R .styleable .RTextView_icon_direction , ICON_DIR_LEFT );
251- //兼容系统原生drawableLeft
252- String namespace = "http://schemas.android.com/apk/res/android" ;//android的命名空间
253- int drawableLeft = attrs .getAttributeResourceValue (namespace , "drawableLeft" , 0 );
254- if (drawableLeft != 0 ) mIconNormalLeft = context .getResources ().getDrawable (drawableLeft );
255- int drawableTop = attrs .getAttributeResourceValue (namespace , "drawableTop" , 0 );
256- if (drawableTop != 0 ) mIconNormalTop = context .getResources ().getDrawable (drawableTop );
257- int drawableRight = attrs .getAttributeResourceValue (namespace , "drawableRight" , 0 );
258- if (drawableRight != 0 )
259- mIconNormalRight = context .getResources ().getDrawable (drawableRight );
260- int drawableBottom = attrs .getAttributeResourceValue (namespace , "drawableBottom" , 0 );
261- if (drawableBottom != 0 )
262- mIconNormalBottom = context .getResources ().getDrawable (drawableBottom );
263- int drawableStart = attrs .getAttributeResourceValue (namespace , "drawableStart" , 0 );
264- if (drawableStart != 0 ) {
265- if (TextViewUtils .isRight2Left ()) {
266- mIconNormalRight = context .getResources ().getDrawable (drawableStart );
267- } else {
268- mIconNormalLeft = context .getResources ().getDrawable (drawableStart );
269- }
270- }
271- int drawableEnd = attrs .getAttributeResourceValue (namespace , "drawableEnd" , 0 );
272- if (drawableEnd != 0 ) {
273- if (TextViewUtils .isRight2Left ()) {
274- mIconNormalLeft = context .getResources ().getDrawable (drawableEnd );
275- } else {
276- mIconNormalRight = context .getResources ().getDrawable (drawableEnd );
277- }
217+ //兼容逻辑(优先级 drawableStart > drawableLeft > icon_normal_left)
218+ if (!TextViewUtils .isRight2Left ()) {
219+ if (drawableStart != null ) drawableLeft = drawableStart ;
220+ if (drawableEnd != null ) drawableRight = drawableEnd ;
221+ } else {
222+ if (drawableEnd != null ) drawableLeft = drawableEnd ;
223+ if (drawableStart != null ) drawableRight = drawableStart ;
278224 }
225+ if (drawableLeft != null ) mIconNormalLeft = drawableLeft ;
226+ if (drawableRight != null ) mIconNormalRight = drawableRight ;
227+ if (drawableTop != null ) mIconNormalTop = drawableTop ;
228+ if (drawableBottom != null ) mIconNormalBottom = drawableBottom ;
279229
280230
281231 //text
0 commit comments