In Android development, the application of beautiful UI often has a large number of XML files. For example, we want to add a selector to a button, if the background is not a picture, you have to write three XML files, respectively:
Edit_focused.xml
<?XML version= "1.0" encoding= "Utf-8"?><Shapexmlns:android= "Http://schemas.android.com/apk/res/android" > <CornersAndroid:radius= "3dip" /> <GradientAndroid:angle= "All"Android:endcolor= "#ffffff"Android:startcolor= "#000000"Android:type= "Linear" /></Shape>
Edit_normal.xml
<?XML version= "1.0" encoding= "Utf-8"?><Shapexmlns:android= "Http://schemas.android.com/apk/res/android" > <CornersAndroid:radius= "5dip" /> <GradientAndroid:angle= "0"Android:endcolor= "#000000"Android:startcolor= "#ffffff"Android:type= "Linear" /></Shape>
Selector_edit.xml
<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> <Itemandroid:drawable= "@drawable/edit_focus"android:state_pressed= "true"></Item> <Itemandroid:drawable= "@drawable/edit_normal"></Item></selector>
A button selector three XML, so that the number of XML files is too difficult to think of, in fact, we can combine the three files into one, write together, so that it can greatly reduce the number of confusing XML files. As follows:
Selector_edit.xml
<?XML version= "1.0" encoding= "Utf-8"?><selectorxmlns:android= "Http://schemas.android.com/apk/res/android"> <Itemandroid:state_pressed= "true"> <Shape> <CornersAndroid:radius= "3dip" /> <GradientAndroid:angle= "All"Android:endcolor= "#ffffff"Android:startcolor= "#000000"Android:type= "Linear" /> </Shape> </Item> <Item> <Shape> <CornersAndroid:radius= "5dip" /> <GradientAndroid:angle= "0"Android:endcolor= "#000000"Android:startcolor= "#ffffff"Android:type= "Linear" /> </Shape> </Item></selector>
Used exactly the same as above. But the number of XML files is much reduced.
< Button Android:layout_width = "Wrap_content" android:layout_height= "Wrap_content" android:layout_centerhorizontal= "True" android:layout_centervertical= "true" Android:background = "@drawable/selector_anotate_icon" android:text= "@string/btn_text" />
Reduce the number of XML files