Travel to Android 16th use of various resources in android and travel to android

Source: Internet
Author: User

Travel to Android 16th use of various resources in android and travel to android

Use of various resources in android:

In android development, reasonable use of various resources should be defined in their respective xml for reuse;

String Resource: strings. xml, referenced in xml: @ string/XXX, referenced in java code: R. string. XXX

Style Resource: styles. xml, referenced in xml: @ style/XXX, referenced in java code: R. style. XXX

Image Resource: colors. xml, referenced in xml: @ color/XXX, referenced in java code: R. color. XXX

Size Resource: dimens. xml, referenced in xml: @ dimen/XXX, referenced in java code: R. dimen. XXX


According to the definition of each resource, the implementation is simple:

Modify the page layout background: android: background = "# f0f0e0". You can also reference the resource defined in color. xml.

1. Create a 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 color of TextView and Button is defined above. 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 styles. xml file in values: defines a unified style, which can be used in multiple applications.

<?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>

Reference in xml: Define style for TextView: @ style/content_style

<TextView android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: text = "test style" style = "@ style/content_style"/>

3. Add a background image to the Button: android: background = "@ drawable/btn_bg1". btn_bg1 indicates the image resource placed in drawable-hdpi, ldpi, and mdpi, hdpi, ldpi, and mdpi represent high resolution, low resolution, and Medium Resolution, which are determined by the screen resolution of the android mobile phone;

4. Sometimes, we have such a requirement that the button initialization should be yellow-green. When we click it, it will become a red background, similar to the following requirements:


After clicking:

At this time, we can use the selector to define in xml. selector, we create a folder named drawable under res, and then write the corresponding xml file of selector:

<?xml version="1.0" encoding="utf-8"?><selector  xmlns: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 above definition:

Button to press the lock to display the image:

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

Focus on the displayed image:

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

Default image:

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

There are many attributes for us to choose from!

We reference it in xml: android: background: @ drawable/Corresponding selector file name. xml

Java code: R. drawable. The selector file name. xml

5. Define a rectangle button with rounded corners: it can also be selected based on selector, but it can also define multiple shape attributes. shape indicates defining multiple shapes, which are also used, it can bring a rich experience to our interface:

<?xml version="1.0" encoding="utf-8"?><selector  xmlns:android="">  <item android:state_selected="true"><shape>    <gradient android:angle="270" android:startColor="#A5D245"        android:endColor="#99BD4C"/>    <size android:height="60dp" android:width="320dp"/>    <corners android:radius="8dp"/></shape>        </item>  <item android:state_pressed="true"><shape>    <gradient android:angle="270" android:startColor="#A5D245"        android:endColor="#99BD4C"/>    <size android:height="60dp" android:width="320dp"/>    <corners android:radius="8dp"/></shape>        </item>  <item><shape>    <gradient android:angle="270" android:startColor="#A8C3B0"        android:endColor="#C6CFCE"/>    <size android:height="60dp" android:width="320dp"/>    <corners android:radius="8dp"/></shape>        </item></selector>

Attribute definition in shape:

<Shape> android: shape = ["rectangle" | "oval" | "line" | "ring"]

Rectagle rectangle, oval elliptic, line horizontal line, ring

<Shape> common attributes of subnodes:

<Gradient> gradient

Android: startColor start color

Android: endColor end color

Android: angle gradient angle. 0 indicates from top to bottom, 90 indicates from left to right, and the default value is 0, which is an integer multiple of 45;

Android: type gradient style liner linear gradient radial Ring gradient sweep

<Solid> Fill

Android: color filled color

<Stroke> stroke

Android: width stroke width

Android: color stroke color

Android: dashWidth indicates the horizontal line width '-'.

Android: dashGap indicates the distance between '-' and '-'.

<Corners> rounded corner

Android: the larger the radius value of the radius rounded corner, the more rounded corner.

Android: top-right corner radius of topRightRadius

Android: bottomLeftRadius right bottom corner radius
Android: top left corner radius of topLeftRadius

Android: bottomRightRadius left bottom corner radius

In the project, we still have a lot of resources that need to be searched and used by ourselves !!

How can I find the resource file tabWidgetStylexml in android?

If you are talking about the tabWidgetStyle in "android: attr/tabWidgetStyle", it seems that it is not defined in a separate XML file, but included in a total style XML.
If you have downloaded the SDK, you can find it in this file: platforms \ android-XX \ data \ res \ values \ styles. xml.
You may not be able to find tabWidgetStyle in this file. Instead, you can only find the Widget. TabWidget. That's because tabWidgetStyle is defined as a Widget. TabWidget in themes. xml.
Some system resource files can also be found under platforms \ android-xx \ data \ res.

Storage location and reading method of original resources in android

Assets to store resources that do not need to be compiled
Images, videos, text, and things to be parsed under res are generated in R.

Related Article

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: 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.