User Interface (the UI) is any graphical interface that can be presented to the user and interact with the user. Android provides a number of pre-defined UI components (a variety of pre-built UI component), such as layout resources, but Android also provides a special UI model such as dialogs, notifications, menus and so on.
From this article, you'll learn about the various UI resources for Android and how to customize the UI resources. This article will cover:
You can also refer to these blog posts:
"Say Goodbye to the Menu Button";
"New Layout Widgets:space and GridLayout";
"Customizing the Action Bar";
"Horizontal View Swiping with Viewpager".
or these training:
"Implementing effective Navigation";
"Designing for Multiple Screens";
"Improving Layout Performance".
Styles and Themes (styles and Themes)
A style resource is a collection of properties that specify the look and format of a view or a window (a style is a collection of properties that specify the looks and format for a V Iew or window. A style can specify properties), such as height, padding, font color, font size, background color, etc. (such as height, padding, font color, font size, background col Or, and much more). The style resource is defined in the XML resource to differentiate the layout resource.
UI controls that have the same or similar appearance can share the same style resource as follows:
<TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#00FF00" android:typeface="monospace" android:text="@string/hello" />
Extract some of the attributes from the above TextView, define it in the style resource, and name the resource Codefont so that other textview can apply the resource, and these TextView styles will remain consistent.
<TextView style="@style/CodeFont" android:text="@string/hello" />
The theme resource is a special style resource, except that the theme resource acts on the entire activity or application, not on the View or window (a theme is a style applied to an entire Activity or application, rather than an individual View). The use of a style as a theme resource on an activity or application is actually a function of all the controls in the activity or application. For example, if you use the style resource Codefont as a theme resource on the activity, all the controls in the activity will have green (#00FF00) and equal-width fonts (monospace font).
Defining styles (Defining styles)
The style should be defined in the XML file under the Res/values/directory, with the file name arbitrarily (the name of the XML files is arbitrary). <resources>
must be the root tag of the style resource in the file. The root tag contains the style resource that you need to customize, <style>
including the tag. where <style>
the attribute name in the label is not default, it specifies the name of the style resource, such as the resource that needs to be referenced in the program, then refers to the content in name. Each <item>
label specifies a property of the style, and the following is a custom style example:
<?xml version= "1.0" encoding= "Utf-8"?><resources> <style name="Codefont" parent="@android: style/ Textappearance.medium "><Item name="Android: Layout_width ">fill_parent</Item> <Item name="Android: Layout_height ">wrap_content</Item> <Item name="Android: TextColor ">#00FF00</Item> <Item name="Android: Typeface ">monospace</Item></style></Resources>
You can reference the style resource in a different location as a @style/codefont. The <style>
Parent property of a label is not required, its value is a resource ID, and a reference is made to an alternative style, which means that now the custom style inherits all the properties of this reference style. You can also override the parent style property that you want to replace in your own style.
If you need to apply a style to an activity or application, that style becomes theme, and its definition is no different than the style that works on the view (exactly the same as a style for a View), just a different term.
Inheritance (inheritance)
<parent>
properties make it easy to inherit from a ready-made style resource, where you can add or modify certain properties. Android provides a number of style resources, such as a style resource that provides a decorative font: textappearance, you can inherit it directly, and modify it:
<style name="GreenText" parent="@android:style/TextAppearance"> <item name="android:textColor">#00FF00</item> </style>
If you need to inherit a custom style resource, you do not need to use a <parent>
property. You only need to prefix the inherited style resource name in the Name property of the newly defined style resource with ". "Separate:
<style name="CodeFont.Red"> <item name="android:textColor">#FF0000</item> </style>
For example, to inherit the custom Codefont style now, the new custom style name is red, so just name it: codefont.red. and referenced in other locations in a @style/codefont.red manner.
If you need to continue to inherit the style above, name can only be written in a chained way:
<style name="CodeFont.Red.Big"> <item name="android:textSize">30sp</item> </style>
! Note: This type of chained style inheritance is only applicable to inheriting your custom style resources, and you will need to use attributes if you want to inherit the style resource that comes with Android <parent>
.
Styles properties (Style property)
<item>
Labels are used to specify custom properties. You can specify layout_width
properties that are similar to, and textColor
so on. It can be defined by the properties that are specific to the view. These property values can be obtained through r.attr in your code. If you reference a style that contains properties that do not support the control, these properties are ignored.
Some special properties are not supported by any view and can only be applied to theme. In other words, these properties are only supported by activity or application. For example, some properties are used to determine whether to hide the app's title, hide the status bar, or modify the window's background color. These attributes do not belong to any view, they have a common feature, that is, the name begins with window. For example: Windownotitle and Windowbackground properties.
Applying styles and themes in the UI (applying styles and Themes to the UI)
There are two ways to apply a style:
For view, just use the <style>
tag to refer to the style resource;
For activity or application, you need to <activity>
<application>
use the attribute configuration in the corresponding or tag in the Android manifest file android:theme
.
! Note: Applying a style resource to a view only takes effect on the view style, that is, if you apply the style resource to ViewGroup, the resource is not automatically applied to the ViewGroup's child view. You can only refer to the style resource individually for each view, or simply use the style as a theme reference directly in ViewGroup's activity. It is also important to note that the style attribute is not included with the "Android" prefix
Style applied on view (apply a style to a view)
Examples are as follows:
<TextView style="@style/CodeFont" android:text="@string/hello" />
Theme applied on activity or application (apply a theme to an activity or application)
Examples are as follows: (in Androidmanifest.xml)
<application android:theme="@style/CustomTheme">
If you want to define activity as a dialog box, you can refer to the Theme.dialog topic:
<activity android:theme="@android:style/Theme.Dialog">
To make the activity background transparent, you can refer to the theme.translucent topic:
<activity android:theme="@android:style/Theme.Translucent">
To customize a system theme, simply customize it like a style's inheritance:
name="custom_theme_color">#b0b0ff</color>name="CustomTheme" parent="android:Theme.Light"> <itemname="android:windowBackground">@color/custom_theme_color</item> <itemname="android:colorBackground">@color/custom_theme_color</item></style>
! Note: Because the value of the Android:windowbackground property supports only reference types and does not accept literals, the notation is as shown above.
Just apply this custom theme to your activity:
<activity android:theme="@style/CustomTheme">
Select a basic theme for the entire application (select a theme based on platform version)
The new Android version adds some system themes, and in order for these topics to be used in older versions of Android, you should use different suffixes in the directory to differentiate between folders, such as: Android:Theme.Holo.Light is on Android After 3.0 new topics, you need to customize a theme that inherits the theme and put the topic in Res/values-v11/styles.xml:
<style name="LightThemeSelector" parent="android:Theme.Holo.Light"> ...</style>
The following topics are defined in the default directory (Res/values/styles.xml):
<style name="LightThemeSelector" parent="android:Theme.Light"> ...</style>
Because the Android:Theme.Light theme is also supported before Android 3.0, the theme is loaded when the device version is less than 3.0.
You can get all the collection of properties for the topic through R.styleable.theme.
Android Official Document User Interface (Styles and Themes)