Android themes and styles

Source: Internet
Author: User

I. Style

A style is a set of attributes, such as defining attributes such as fontColor, fontSize, layout_width, and layout_height. It is stored in an XML file as an independent resource file and the style name is set.

Android Style is similar to the cascade Style CSS design concept in web design. It can separate the design from the content and easily inherit, overwrite, and reuse the content.

 


1. Style not used

[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>

<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textColor = "#00FF00"
Android: text = "@ string/hello"/>

<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>
 
<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>
 
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>

<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: textColor = "#00FF00"
Android: text = "@ string/hello"/>

<TextView
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"/>

<Button
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: text = "@ string/hello"
/>

</LinearLayout> the preceding Interface contains three controls, two textviews and one Button. Both layout_width and layout_height are the content of the wrap_content package.

Next, let's take a look at how to use styles for improvement.

 


2. Use Style

First, create a Style XML resource file under res/values/. The name of the created Style resource file is styles. xml, which can be customized by yourself.

Styles. xml contains the following content:

[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "wrap_content">
<Item name = "android: layout_width"> wrap_content </item>
<Item name = "android: layout_height"> wrap_content </item>
</Style>
</Resources>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "wrap_content">
<Item name = "android: layout_width"> wrap_content </item>
<Item name = "android: layout_height"> wrap_content </item>
</Style>
</Resources> the name attribute in the style label is similar to the class name in CSS. the name in the item label corresponds to the name of the attribute, and the text value in the item label pair corresponds to the attribute value.

 


Style:

[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>

<TextView
Style = "@ style/wrap_content"
Android: textColor = "#00FF00"
Android: text = "@ string/hello"/>

<TextView
Style = "@ style/wrap_content"
Android: text = "@ string/hello"/>
 
<Button
Style = "@ style/wrap_content"
Android: text = "@ string/hello"
/>
 
</LinearLayout>
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>

<TextView
Style = "@ style/wrap_content"
Android: textColor = "#00FF00"
Android: text = "@ string/hello"/>

<TextView
Style = "@ style/wrap_content"
Android: text = "@ string/hello"/>

<Button
Style = "@ style/wrap_content"
Android: text = "@ string/hello"
/>

</LinearLayout>

3. style inheritance

There are two ways to implement inheritance. One is to use the parent attribute of the style, and the other is to use naming rules similar to those in CSS.

1. parent attributes

Modify styles. xml

[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "wrap_content">
<Item name = "android: layout_width"> wrap_content </item>
<Item name = "android: layout_height"> wrap_content </item>
</Style>

<Style name = "inherit" parent = "wrap_content">
<Item name = "android: textColor"> #00FF00 </item>
</Style>
</Resources>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "wrap_content">
<Item name = "android: layout_width"> wrap_content </item>
<Item name = "android: layout_height"> wrap_content </item>
</Style>
 
<Style name = "inherit" parent = "wrap_content">
<Item name = "android: textColor"> #00FF00 </item>
</Style>
</Resources> Add a style named inherit and inherit the style named wrap_content. That is, inherit has the attribute parameters defined in the wrap_content style.

Reference Method: style = "@ style/inherit"

 


II. Implementation through naming rules

[Html] <? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "wrap_content">
<Item name = "android: layout_width"> wrap_content </item>
<Item name = "android: layout_height"> wrap_content </item>
</Style>

<Style name = "wrap_content.inherit">
<Item name = "android: textColor"> #00FF00 </item>
</Style>
</Resources>
<? Xml version = "1.0" encoding = "UTF-8"?>
<Resources>
<Style name = "wrap_content">
<Item name = "android: layout_width"> wrap_content </item>
<Item name = "android: layout_height"> wrap_content </item>
</Style>
 
<Style name = "wrap_content.inherit">
<Item name = "android: textColor"> #00FF00 </item>
</Style>
</Resources> use the "." sign to implement inheritance.

Reference Method: style = "@ style/wrap_content.inherit"


Ii. Themes

You can edit AndroidManifest. xml to set styles for all the activities in the application or for a specific Activity.

1. Set the topic of all Activity activities in the application.

[Html] <application android: theme = "@ style/wrap_content">
<Application android: theme = "@ style/wrap_content"> in this way, the package layout is used by default for all components in the Activity of the application.

 


2. Set a specified Activity topic
[Java] <activity android: theme = "@ style/wrap_content">
<Activity android: theme = "@ style/wrap_content">
In addition, android provides many built-in theme styles. For example, Theme. Dialog and Theme. Translucent. Easy to use

[Html] <activity android: theme = "@ android: style/Theme. Dialog">
<Activity android: theme = "@ android: style/Theme. Dialog">

 

 

 

Style properties reference: http://android.toolib.net/reference/android/R.attr.html
Theme property parameters: http://android.toolib.net/reference/android/R.styleable.html#Theme


Style and theme reference: http://android.toolib.net/guide/topics/ui/themes.html

 


From God's blog
 

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