Home  >  Article  >  Backend Development  >  Sample code sharing to reduce the number of XML files

Sample code sharing to reduce the number of XML files

黄舟
黄舟Original
2017-03-20 16:40:051526browse

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn