Reduce the number of XML files and xml files

Source: Internet
Author: User

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" />

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.