Home > Article > Backend Development > Sample code sharing to reduce the number of XML files
In android development, applications that make beautiful UI often have a huge number of xml files. For example, if we want to add a selector to a Button, if the background is not a picture, we have to write three xml files, which are:
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>
A button selector requires three xmls. Calculated in this way, it is too difficult to reduce the number of xml files. In fact, we can Merge three files into one and write them together, which can greatly reduce the dazzling number of xml files. 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. But the number of xml files is reduced a lot.
<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" />
The above is the detailed content of Sample code sharing to reduce the number of XML files. For more information, please follow other related articles on the PHP Chinese website!