Use of style and theme in Android

Source: Internet
Author: User
Document directory
  • Theme:

 

 

Original post: http://henzil.easymorse.com /? P = 364

 

More and more Internet enterprises are deploying their clients on the Android platform. To improve user experience, these clients are well-organized and beautiful ....... android style design is one of the keys to improve user experience. The style on Android is divided into two aspects:

  1. Theme is for the form level and changes the form style;
  2. The style is for the form Element Level, and changes the style of the specified control or layout.

Themes. XML and style. XML (located in/base/CORE/RES/values/) contains many styles defined by the system. We recommend that you select an appropriate style and then inherit and modify it.

 

 

A style is a set of attributes in a view, including height, padding, font color, and background. A style is defined separately in an XML file, similar to a CSS role in a Web page, separate the design and content for ease of modification and reuse.

Define style:

The style file must be saved in the Res/values directory. The file name must be an XML file and the root mark of the sytle file must be <resources>. The following is a simple example:

 

 

Main. xml file code:

 

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <br/> <textview <br/> style = "@ style/codefont" mce_style = "@ style/codefont" <br/> Android: TEXT = "test style"/> <br/> </linearlayout> 

The declared style is codefont and corresponds to the style name in the style file. The mystyle. xml file defines that the style name is codefont:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> <br/> <MCE: style name = "codefont" parent = "@ Android: style/textappearance. Medium"> <! -- </P> <p> <item name = "Android: layout_width"> fill_parent </item> <br/> <item name = "Android: layout_height "> wrap_content </item> <br/> <item name =" Android: textcolor ">#00ff00 </item> <br/> <item name =" Android: typeface "> monospace </item> </P> <p> --> </MCE: style> <style name =" codefont "parent =" @ Android: style/textappearance. medium "mce_bogus =" 1 "> <br/> <item name =" Android: layout_width "> fill_parent </item> <br/> <item name =" Android: layout_height "> wrap_content </item> <br/> <item name =" Android: textcolor ">#00ff00 </item> <br/> <item name =" Android: typeface "> monospace </item> <br/> </style> <br/> </resources> 

The parent attribute indicates that the style can be inherited, and some attributes of the parent style can be overwritten.

 

 

There are two methods to inherit a style:

  • You can use the parent attribute to inherit the style defined by Android. For example:<MCE: style name = "greentext" parent = "@ Android: style/textappearance"> <! -- </P> <p> <item name = "Android: textcolor"> #00ff00 </item> <br/> --> </MCE: style> <style name = "greentext" parent = "@ Android: style/textappearance" mce_bogus = "1"> <br/> <item name = "Android: textcolor "> #00ff00 </item> </style> 
  • Inherits textappearance from Android and overwrites the Android: textcolor attribute.
  • If you want to inherit a custom style, you do not need to use the parent attribute. As long as the name of the style starts with the name of the style to be inherited, it is separated. Note: This method applies only to custom style inheritance.

<MCE: style name = "codefont. Red"> <! -- </P> <p> <item name = "Android: textcolor"> # ff0000 </item> <br/> --> </MCE: style> <style name = "codefont. red "mce_bogus =" 1 "> <br/> <item name =" Android: textcolor "> # ff0000 </item> </style> 

 

 

The new style inherits codefont, and the main. XML in the above example is modified:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <br/> <textview <br/> style = "@ style/codefont. red "mce_style =" @ style/codefont. red "<br/> Android: text =" test style "/> <br/> </linearlayout> 


The effect is as follows. The font color is changed to Red:

Style can be inherited at multiple levels:

<MCE: style name = "codefont. Red. Big"> <! -- </P> <p> <item name = "Android: textsize"> 30sp </item> <br/> --> </MCE: style> <style name = "codefont. red. big "mce_bogus =" 1 "> <br/> <item name =" Android: textsize "> 30sp </item> </style> 

The font size increases as follows:


For more attributes of sytle, see R. ATTR In the android package. Note that not all views support the defined style attributes. If the custom sytle contains attributes not supported by the view, the program automatically ignores them.

 

 

 

Theme:

 

If you declare a style as theme, you must configure the Android: Theme attribute of <activity> or <Application> In the mainfest file.

Use the custom style as the theme of the application:

Modify mystyle. XML:

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <resources> <br/> <MCE: style name = "codefont"> <! -- </P> <p> <item name = "Android: textsize"> 20sp </item> <br/> <item name = "Android: typeface "> monospace </item> </P> <p> --> </MCE: style> <style name = "codefont" mce_bogus = "1"> <br/> <item name = "Android: textsize "> 20sp </item> <br/> <item name =" Android: typeface "> monospace </item> <br/> </style> <br/> </resources> 

Add the Android: Theme attribute to the mainfest application:

<Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name" <br/> Android: theme = "@ style/codefont"> 

All text fonts in the application will change. The effect is as follows:


 

Use the Android: Theme attribute in each <activity> tag:

<Activity Android: Name = ". mainactivity" <br/> Android: Label = "@ string/app_name" Android: theme = "@ style/codefont"> 

Android: Theme: You can also configure the existing theme in Android:

<Activity Android: theme = "@ Android: style/theme. translucent"> 

To adjust the theme defined by Android, you can use a custom style, for example:

<Color name = "custom_theme_color"> # b0b0ff </color> <br/> <MCE: style name = "customtheme" parent = "Android: theme. Light"> <! -- </P> <p> <item name = "Android: windowbackground"> @ color/custom_theme_color </item> <br/> <item name = "Android: colorbackground "> @ color/custom_theme_color </item> <br/> --> </MCE: style> <style name =" customtheme "parent =" Android: theme. light "mce_bogus =" 1 "> <br/> <item name =" Android: windowbackground "> @ color/custom_theme_color </item> <br/> <item name =" Android: colorbackground "> @ color/custom_theme_color </item> </style> 

The effect is as follows:


 

 

 

 

Theme with a level lower than 11:

 

 

1. Theme:

It indicates the default state, that is, if theme does not fill in any attribute here, the default value is theme.

Original API:

The default system theme. This is the theme used for activities that have not explicitly set their own theme.

You can count on this being a dark background with light text on top, but shocould try to make no other assumptions about its appearance. in particle, the text inside of widgets using this theme may be completely different, with the widget container being a light color and the text on top of it a dark color.

As follows:

 

 

 

1.1,Theme_nodisplay

It means that nothing is displayed. This method is applicable when only the activity is run, but nothing is displayed.

The original API text is as follows:

Default theme for activities that don't actually display a UI; that is, they finish themselves before being resumed.

 

 

As follows:

 

 

 

1.2. theme_notitlebar

The style of the background topic without a title bar. If it is not set by default, the black background is displayed.

Original API:

Variant of the default (dark) theme with no title bar

As follows:

 

 

 

1.3. theme_notitlebar_fullscreen

The style of the background topic without a title bar and full screen. The default is a black background.

Original API:

Variant of the default (dark) theme that has no title bar and fills the entire Screen

As follows:

 

 

 

2. theme_black:

It indicates a black background by default.

The original API text is as follows:

Special variation on the default theme that ensures the background is completely black. This is useful for things like image viewers and media players. If you want the normal (dark background) Theme doNotUse this, useTheme.

As follows:

 

 

 

2.1. theme_black_notitlebar:

The style of the black background subject without a title bar.

Original API:

Variant of the black theme with no title bar

As follows:

 

 

 

2.2, theme_black_notitlebar_fullscreen

It means that the black background theme has no title bar and a full screen style.

Original API:

Variant of the black theme that has no title bar and fills the entire Screen

As follows:

 

 

 

3. theme_light

It indicates that the background is highlighted by default, which is opposite to the above-mentioned black background theme_black.

Original API:

Theme for a light background with dark text on top. set your activity to this theme if you wowould like such an appearance. as with the default theme, you should try to assume little more than that the background will be a light color.

As follows:

 

3.1. theme_light_notitlebar

It indicates that the style with no title bar is highlighted, which is opposite to theme_black_notitlebar.

Original API:

Variant of the Light theme with no title bar

As follows:

 

 

 

 

3.2, theme_light_notitlebar_fullscreen

Indicates the style with no title bar and full screen display for the highlighted background topic, which is opposite to theme_black_notitleba_fullscreenr.

Original API:

Variant of the Light theme that has no title bar and fills the entire Screen

As follows:

 

 

 

 

4. theme_dialog

The dialog box style changes the entire activity to the dialog box style.

Original API:

Default theme for dialog windows and activities, which is used byDialogClass. this changes the window to be floating (not fill the entire screen), and puts a frame around its contents. you can set this theme on an activity if you wocould like to make an activity that looks like a dialog.

The Custom size is required here. Otherwise, the display is incomplete.

 

 

 

5. theme_inputmethod

6,Theme_panel

Delete all unnecessary window decorations and fill in the content in an empty rectangle. The function scope is equivalent to removing all the elements in the dialog, but an empty rectangle, this is the default style.

Original API:

Default dark theme for panel windows. this removes all extraneous window decorations, So you basically have an empty rectangle in which to place your content. it makes the window floating, with a transparent background, and turns off dimming behind the window.

The Custom size is required here. Otherwise, the display is incomplete.

 

 

 

6.1,Theme_light_panel

Delete all unnecessary window decorations and fill in the content in an empty rectangle. The function scope is equivalent to removing all the elements in the dialog, but an empty rectangle, the default is the light style.

Original API:

Default light theme for panel windows. this removes all extraneous window decorations, So you basically have an empty rectangle in which to place your content. it makes the window floating, with a transparent background, and turns off dimming behind the window.

The Custom size is required here. Otherwise, the display is incomplete.

 

 

 

 

7,

Theme_wallpaper

It indicates that the wallpaper is used as the topic. The default status is displayed.

Original API:

Default theme for Windows that want to have the user's selected wallpaper appear behind them.

As follows:

 

7.1. theme_wallpapersettings

It means to use wallpaper as the theme. By default, after the previous interface is dimmed, it is used as the theme. (I am wondering why the last interface is dimmed instead of the wallpaper theme ?)

Original API:

Theme for a wallpaper's setting activity that is designed to be on top of a dark background.

As follows:

 

7.2. theme_light_wallpapersettings

It indicates that the wallpaper is used as the theme, and the default light state is used.

Original API:

Theme for a wallpaper's setting activity that is designed to be on top of a light background.

As follows:

 

7.3 theme_wallpaper_notitlebar

The theme is made with a wallpaper without a title bar.

Original API:

Variant of the translucent theme with no title bar

As follows:

 

7.4. theme_wallpaper_notitlebar_fullscreen

It means that the theme is made with wallpaper, and there is no title bar, and the screen is displayed

Original API:

Variant of the translucent theme that has no title bar and fills the entire Screen

As follows:

 

8. theme_translucent

Indicates the background in the translucent state. The screen before the activity is run is used as the style when the activity is run.

Original API:

Default theme for translucent activities, that is windows that allow you to see through them to the windows behind. This sets up the translucent flag and appropriate animations for your windows.

As follows:

 

8.1, theme_translucent_notitlebar

It indicates the background without a title bar in the translucent state. The screen before the activity is run as the translucent state as the style when the activity is run.

Original API:

Variant of the translucent theme with no title bar

As follows:

 

8.2, theme_translucent_notitlebar_fullscreen

It indicates that the screen before the activity is run as the style when the activity is running, and there is no title bar and full screen background in the translucent state.

Original API:

Variant of the translucent theme that has no title bar and fills the entire Screen

As follows:

 

 

 

 

 

 

 

 

 

 

 

 

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.