Pro Android Learning Note (24): User interface and Control (page): Style and theme

Source: Internet
Author: User
Tags gettext

Static format

Set a static style in Res/values, you can use the HTML format to set a static style in a resource <i> <u> <b> <sup> <sub> <strike> <big> <small> <monospace>.

<string name= "ui_styletext_1" ><i>Static</i> style <u>in</u> a <b>textview</b . <strike>strike</strike></string>

We experiment in XML, and we also look at the settings for other effects.

<textview android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:autolink= "Web|email"
android:text= "Visit for more help on using Android."
Android:minlines= "3"
Android:typeface= "Serif"/>
<textview android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "@string/ui_styletext_1"/>

Set in code

Set the style in the code through spannable. As shown in. For EditText, we set the Android:inputtype= "Text|textautocorrect|textautocomplete|textmultiline" in the XML, so a red underline appears, which indicates a spelling error.

TextView needs to specify Buffertype before it can get the Spannable object through GetText ().
TextView TV = (TextView) Findviewbyid (R.ID.UI_STYLE_TV);
Tv.settext ("This text was stored in a spannable", TextView.BufferType.SPANNABLE);
Spannable Spantv = (spannable) tv.gettext ();

by Setspan (), the character of this example 0~7 is processed in a range of text
Spantv.Setspan(New Backgroundcolorspan (,0, 7, spannable.span_exclusive_exclusive);
Spantv.Setspan(New Stylespan (,0, 7, spannable.span_exclusive_exclusive);

EditText can get direct access to spannable objects via GetText ()
EditText et = (EditText) Findviewbyid (;
Et.settext ("Styling the content of an EditText dynamically");
spannable spanet = (spannable) et.gettext ();
Spanet.Setspan(New Backgroundcolorspan (, 0, 7, spannable.span_exclusive_exclusive);
Spanet.Setspan(New Stylespan (, 0, 7, spannable.span_exclusive_exclusive);


If the same format is used for more than one control, each control is written again, cumbersome, can be defined as a style, and the style is set in the control. The style is defined in res/values/, as shown below.

<?xml version= "1.0" encoding= "Utf-8"?>
<resources xmlns:android= "Http://" >
<!--Example 1: Define a style with the name "Myerrortext", set the color and font, and, incidentally, set width and height without bothering to write every time--
<style name= "Myerrortext" >
<item name= "Android:layout_width" >match_parent</item>
<item name= "Android:layout_height" >wrap_content</item>
<item name= "Android:textcolor" > #FF0000 </item>
<item name= "Android:typeface" >monospace</item>
<!--example 2:style tree hierarchy: Style provides a convenient hierarchy that can pass "." One layer at a-level. To extend-
<style name= "Myerrortext.danger">
<item name= "Android:textstyle" >bold</item>
<!--Example 3: For extending the style of Android, you cannot use ".", but use the parent. Android comes with a style that can be viewed in sdk\platforms\android-xx\data\res\values\styles.xml-
<style name= "MyText"parent= "@android: Style/textappearance.small">
<item name= "Android:textcolor" > #FF00FF </item>

The contents of the layout XML file are as follows:

<--calls style, unlike other properties, without the front Android:-
<textviewstyle= "@style/myerrortext"
android:text= "Error:no Error here."/>
<--calls the style of the hierarchy---
<textviewstyle= "@style/myerrortext.danger"
android:text= "Fatal error:test ..."/>
<--Call Android System custom style-->
<textviewstyle= "@android: Style/textappearance.holo"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "Android Default Style:TextAppearance.Holo"/>
<--Test Style Example 3 effect--
<textview style= "@style/mytext"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:text= "Information:my text"/>
<--for system-defined theme, we can use only one of these properties--
<edittext android:id= "@+id/ui_style_ed2"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Android:textcolor= "? Android:textcolorsecondary"
android:text= "@string/ui_styletest"/>


In the above example, theme has been mentioned. You can avoid the description of control properties in a control by using style, which provides great convenience if the modifications can be modified in only one place, but still requires a style designation in each control. If you want attributes to be useful throughout the activity or the entire application, you can use theme. Theme and style are conceptually similar and are described in the same style as the res/vaules/XML file when defining attributes. As the following example. Android system custom theme defined in Sdk\platforms\android-xx\data\res\values\themes.xml.

<style name= "MyTheme" parent= "@android: Style/theme" >
<item name= "Android:textcolor" > #666666 </item>

The theme is set in the Androidmanifest.xml.

For activity:
<activity ... android:theme= "@style/mytheme" .../>
For application:
<application .... android:theme= "@style/mytheme" .../>

RELATED Links: My Android development related articles

Pro Android Learning Note (24): User interface and Control (page): Style and theme

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.