Android Tour 16 use of various resources in Android

Source: Internet
Author: User

The use of various resources in Android:

In Android development, the rational use of various resources should be defined in the respective XML in order to re-use;

String resource: References in Strings.xml,xml: @string/xxx,java Code: R.STRING.XXX

Style resources: Styles.xml,references in XML: @style/xxx,java in code: R.STYLE.XXX

Image resource: Colors.xml,Reference in XML: @color/xxx,java in code: R.COLOR.XXX

Dimension resource: Dimens.xml, referenced inxml: @dimen/xxx,java in code: R. Dimen. XXX

.............

Based on the definition of each resource, the implementation is simple:

Modify Page layout background: android:background= "#f0f0e0", or reference a resource defined in Color.xml

1. Create a new Colors.xml file in the values folder:

<?xml version= "1.0" encoding= "Utf-8"? ><resources><color name= "Tv_text_color" > #FF33CC </color ><color name= "Btn_text_color" > #0033CC </color></resources>

The font colors in TextView and buttons are defined above, and the page uses: @color/tv_text_color

   <textview        android:layout_width= "fill_parent"        android:layout_height= "wrap_content"        android:text= "Test font style"        android:textcolor= "@color/tv_text_color"/>


2. Create a new Styles.xml file in values: Defines a unified style style that can be used in multiple places in the application

<?xml version= "1.0" encoding= "Utf-8"? ><resources><style name= "Apptheme" parent= "android:Theme.Light "></style><style name=" Content_style ">        <item name=" Android:minheight ">50dp</item>        <item name= "android:gravity" >left|center</item>        <item name= "Android:textcolor" ># 000000</item>        <item name= "android:textsize" >15sp</item>        <item name= "Android: Linespacingextra ">2dp</item>        <item name=" Android:background "> #bfbfbf </item>    </ Style></resources>

Referencing in XML: Defines a style style for TextView: @style/content_style

    <textview        android:layout_width= "fill_parent"        android:layout_height= "wrap_content"        android:text = "Testing style style"        style= "@style/content_style"/>

3. Add background image to button: android:background= "@drawable/btn_bg1", where btn_bg1 is a picture resource placed in drawable-hdpi, ldpi, MDPI, hdpi, ldpi, MDPI represent high resolution, low resolution, and medium resolution, respectively, according to the screen resolution of the Android phone;

4, sometimes, we have such a demand, the button is initialized to yellow green, when we click on the red background, similar to the following requirements:

Initialization

After clicking:

At this point, we can use the selector in the XML to define, selector, we create a new folder under Res drawable, and then write our selector corresponding XML file:

<?xml version= "1.0" encoding= "Utf-8"? ><selector  xmlns:android= "http://schemas.android.com/apk/res/ Android ">    <item android:state_pressed=" true "android:drawable=" @drawable/btn_bg2 "/>    <item Android:state_focused= "true" android:drawable= "@drawable/btn_bg4"/>    <item android:drawable= "@drawable/ Btn_bg1 "/></selector>

In the definition above:

button to press the lock to display the picture:

<item android:state_pressed= "true" android:drawable= "@drawable/btn_bg2"/>

Focus on the picture shown:

<item android:state_focused= "true" android:drawable= "@drawable/btn_bg4"/>

Pictures displayed by default:

<item android:drawable= "@drawable/btn_bg1"/>

There are a lot of properties for us to choose!

We reference it in XML: Android:background: @drawable/corresponding selector file name. xml

Java code: r.drawable. The corresponding selector file name. xml

5. Define a rectangular button with rounded corners: Also according to selector, but it can also define a variety of shape properties, shape to define a variety of shapes, with a lot of, can give our interface a rich experience:

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android "> <item android:state_selected=" true "><shape> <gradient android:angle=" + "Android:    Startcolor= "#A5D245" android:endcolor= "#99BD4C"/> <size android:height= "60DP" android:width= "320DP"/> <corners android:radius= "8DP"/></shape> </item> <item android:state_pressed= "true" ><s hape> <gradient android:angle= "android:startcolor=" #A5D245 "android:endcolor=" #99BD4C "/> <si Ze android:height= "60dp" android:width= "320DP"/> <corners android:radius= "8DP"/></shape> </ite m> <item><shape> <gradient android:angle= "android:startcolor=" #A8C3B0 "android:endcolor= "#C6CFCE"/> <size android:height= "60DP" android:width= "320DP"/> <corners android:radius= "8DP"/></ Shape> </item></selector> 


The attribute definition in shape explains:

<shape> android:shape=["Rectangle" | "Oval" | "Line" | "Ring"]

where Rectagle rectangle, oval oval, line horizontal straight, ring ring

<shape> Common properties of neutron nodes:

<gradient> gradients

Android:startcolor Start Color

Android:endcolor End Color

Android:angle gradient angle, 0 from top to bottom, 90 for left to right, and 45 for integer times by default to 0;

Android:type gradient Style liner linear gradient radial ring gradient sweep

<solid > Filling

Android:color the color of the fill

<stroke > Strokes

Android:width width of stroke

Android:color Color of strokes

Android:dashwidth = width of '-' horizontal line

Android:dashgap means the distance between the '-' horizontal lines

<corners > Rounded Corners

Android:radius the radius of the fillet is larger, the more rounded the corner

Android:toprightradius Right Upper corner radius

Android:bottomleftradius Corner radius of Right bottom corner

Android:topleftradius left Upper Corner radius

Android:bottomrightradius left Lower Corner radius

We still have a lot of resources in the project to find our own learning and flexible use!!

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.