Reduce the number of XML files and xml files
In android development, there are usually a large number of xml files for beautiful ui applications. For example, we need to add a selector to a Button. if the background is not an image, we need to write three xml files:
Edit_focused.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="3dip" /> <gradient android:angle="90" android:endColor="#ffffff" android:startColor="#000000" android:type="linear" /></shape>
Edit_normal.xml
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="5dip" /> <gradient android:angle="0" android:endColor="#000000" android:startColor="#ffffff" android:type="linear" /></shape>
Selector_edit.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/edit_focus" android:state_pressed="true"></item> <item android:drawable="@drawable/edit_normal"></item></selector>
The selector of a button requires three xml files. In this way, it is too difficult to think about the number of xml files. In fact, we can combine these three files into one and write them together, in this way, the number of dazzling xml files can be greatly reduced in the program. As follows:
Selector_edit.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true"> <shape> <corners android:radius="3dip" /> <gradient android:angle="90" android:endColor="#ffffff" android:startColor="#000000" android:type="linear" /> </shape> </item> <item> <shape> <corners android:radius="5dip" /> <gradient android:angle="0" android:endColor="#000000" android:startColor="#ffffff" android:type="linear" /> </shape> </item></selector>
It is used exactly the same as above. However, the number of xml files is greatly 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" />