Android styles (style) and themes (theme)

Source: Internet
Author: User

The style on Android is divided into two areas:
1,theme is for the form level, change the form style;
2,style is for the form element level, changing the style of the specified control or layout.

Android system Themes.xml and Style.xml (located in the system source code frameworks\base\core\res\res\values\) contains a lot of system-defined style, it is recommended to pick a suitable, and then inherit the changes.

    • A style is a collection of one or more formatting attributes that you can use as a unit in a single element of the layout XML. For example, you can define a style to define the font size and color of the text, and then use it in a particular instance of the view element.
    • A theme is a collection of one or more formatting attributes that you can use as a unit in all the activity in your app or in an activity in your app. For example, you can define a theme that defines a set of colors for the foreground and background of the window frame and panel, and defines the font size and color attributes for the menu, which you can apply to all the activity in your program.
Both style and theme are resources. You can use some of the default style and theme resources provided by Android, and you can customize your own theme and style resources. How to create a new custom style and theme: 1. Create a new file named Style.xml in the Res/values directory. Add a <resources> root node. 2. For each style and theme, add a globally unique name to <style>element, and optionally add a parent class attribute. In the back we can use the name to apply the style, and the parent attribute identifies which style the current style is inherited from. 3. Within the <style> element, affirm one or more <item> Each of the <item> defines a name attribute and defines the value of the style within the element. 4. You can apply resources that are defined in other XML. —————————————————————————————— – The style below is an example of a declaration style: <?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<style name= "Specialtext" parent= "@style/text" >
<item name= "Android:textsize" >18sp</item>
<item name= "Android:textcolor" > #008 </item>
</style>
</resources> as shown above, you can use the <item> element to define a set of formatted values for your style. The property of the name in item can be a string, a color represented by a 16 binary number, or a reference to another resource. Note the parent class property in the <style> element. This property allows you to define a resource, and the current style can be inherited from this resource to a value. You can inherit this style from any resource that contains this style. In general, your resources should inherit the standard style resources of Android directly or indirectly. In this case, you just need to define the values you want to change. In this example, the EditText element demonstrates how to reference the style defined in an XML layout file: <edittext id= "@+id/text1"
style= "@style/specialtext"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
Android:text= "Hello, world!"/> Now the style shown in this EditText component is what we defined in the XML file above. Theme is like style, the theme is still in the <style> element inside the declaration, also in the same way quoted. The difference is that you add a theme to an entire program or activity through the <application> and <activity> elements defined in Android manifest, but the theme cannot be applied to a separate view. Below is an example of a stated theme: <?xml version= "1.0" encoding= "Utf-8"?>
<resources>
<style name= "Customtheme" >
<item name= "Android:windownotitle" >true</item>
<item name= "Windowframe" > @drawable/screen_frame</item>
<item name= "Windowbackground" > @drawable/screen_background_white</item>
<item name= "Panelforegroundcolor" > #FF000000 </item>
<item name= "Panelbackgroundcolor" > #FFFFFFFF </item>
<item name= "Paneltextcolor" >?panelForegroundColor</item>
<item name= "Paneltextsize" >14</item>
<item name= "Menuitemtextcolor" >?panelTextColor</item>
<item name= "Menuitemtextsize" >?panelTextSize</item>
</style>
</resources> Notice we used the @ symbol and? Symbol to apply the resource. The @ symbol indicates that the resources we have applied are defined previously (either in the previous project or in the Android framework). Question mark? Indicates that the value of the resource we are referencing has been defined in the current topic. You can do this by referencing the name defined in <item> ( PaneltextcolorUse the color and as defined in the Panelforegroundcolor)。 This technique can only be used in XML resources. Set themes in manifest to use themes in all of the activity, you can open androidmanifest.xml files, edit <application> tags to include android:theme properties , the value is the name of a subject, as follows:
<application android:theme= "@style/customtheme" >
If you just want some activity in your program to have this theme, you can modify the <activity> tag. There are several built-in resources available in Android, and there are several themes you can switch to without writing on your own. For example, you can use a dialog box theme to make your activity look like a dialog box. The following are defined in manifest:
<activity android:theme= "@android: Style/theme.dialog" >
If you like a theme, but want to make some minor changes, you just need to add the theme as a parent topic. For example, we modify the Theme.dialog theme. Let's inherit theme.dialog to generate a new theme.
<style name= "Customdialogtheme" parent= "@android: Style/theme.dialog" >
After inheriting the Theme.dialog, we can adjust the theme according to our requirements. We can modify the value of each item element defined in Theme.dialog, and then we use Customdialogtheme instead of Theme.dialog in the Android Manifest file. Set a theme in the program if you want, you can load a theme in the activity by using the method Settheme (). Note that if you do this, you should initialize any view before setting the theme. For example, before calling the Setcontentview (View) and inflate (int, viewgroup) methods. This ensures that the system applies the current theme to all UI interfaces. Examples are as follows: protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
...
SetTheme (Android. R.style.theme_light);
Setcontentview (R.layout.linear_layout_3);
If you intend to load the theme of the main interface in your program code, be aware that the theme does not include any animations that the system uses to initiate the activity, which will be displayed before the program starts. In many cases, if you want to apply a theme to your main interface, it seems like a better way to define it in XML.

Copying directly from the top three below will make an error. @ is a description of the system has been defined, @android: style/must be carried.

? android:theme= "@android: Style/theme.dialog" displays an activity as a dialog box mode
Android:theme= "@android: Style/theme.notitlebar" does not show the application title bar
? android:theme= "@android: Style/theme.notitlebar.fullscreen" does not display the application title bar and fullscreen
Android:theme= "theme.light" background for White
Android:theme= "Theme.Light.NoTitleBar" white background with no title bar
Android:theme= "Theme.Light.NoTitleBar.Fullscreen" white background, no title bar, fullscreen
Android:theme= "Theme.black" background black
Android:theme= "Theme.Black.NoTitleBar" black background with no title bar
Android:theme= "Theme.Black.NoTitleBar.Fullscreen" black background, no title bar, fullscreen
? android:theme= "Theme.wallpaper" with system desktop for application background
? android:theme= "Theme.Wallpaper.NoTitleBar" uses the system desktop as the application background with no title bar
? android:theme= "Theme.Wallpaper.NoTitleBar.Fullscreen" with system desktop for application background, no title bar, fullscreen
? android:theme= "Translucent"
? android:theme= "Theme.Translucent.NoTitleBar" translucent, untitled
? android:theme= "Theme.Translucent.NoTitleBar.Fullscreen" translucent, Untitled, fullscreen
? android:theme= "Theme.panel" Panel style display
Android:theme= "Theme.Light.Panel" tablet style display

Android styles (style) and themes (theme)

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.