Reduce the number of XML files

Source: Internet
Author: User

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

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.