Android Programming Application style and topic detailed _android

Source: Internet
Author: User

This article describes the Android programming application style and themes. Share to everyone for your reference, specific as follows:

When you design your program, you can use styles and themes to unify the formatting of various screens and UI elements.

A style is a collection of one or more formatting attributes that you can use as a unit in a single element of a layout XML. For example, you can define a style to define the size and color of 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 of the activity in the application or in an activity in the application. For example, you can define a theme that defines a set of colors for the foreground and background of window frame and panel, and defines the text 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 Android to provide some default style and theme resources, and you can also customize your own theme and style resources.

How to create a new custom style and theme:

1. Create a new file named Style.xml under the Res/values directory. Add a <resources> root node.

2. For each style and theme, add a globally unique name to <style>element, or you can choose to add a parent class attribute. In the back we can use this name to apply the style, and the parent class attribute identifies which style the current style inherits from.

3. Within the <style> element, declare one or more <item>, each <item> defines a name attribute and defines the value of the style within the element.

4. You can apply the resources defined in other XML.

One, 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 the item can be a string, a color represented by a 16-digit, or a reference to another resource.

Note the properties of the parent class in the <style> element. This property allows you to define a resource that the current style can inherit from the resource to the value. You can inherit this style from any resource that contains this style. Typically, your resources should continue to inherit Android's standard style resources directly or indirectly. In that case, you just need to define the value you want to change.

The EditText element in this example 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 of this edittext component is what we define in the XML file above.

Ii. Themes

As with style, themes are still stated in the <style> elements, and are referenced in the same way. The difference is that you add a theme to the entire program or an activity through the <application> and <activity> elements defined in the Android manifest, but the theme is
cannot be applied to a single view.

Below is an example of the subject of the statement:

<?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? Symbols to apply resources. The @ symbol indicates that the resource we are applying is defined previously (either in the previous project or in the Android framework). Question mark? Indicates that the value of the resource we are referencing is defined in the current topic. You can do this by referencing the name defined in <item> (Paneltextcolor the same color as defined in Panelforegroundcolor). This technique can only be used in XML resources.

1. Set a theme in manifest

To use the theme in all of the activity in the application, you can open the Androidmanifest.xml file, edit <application> tag to include the Android:theme attribute, and the value is the name of a topic, as follows:

<application android:theme= "@style/customtheme" >

If you're just trying to get a topic from an activity in your program, you can modify the <activity> tag.

There are several built-in resources available in Android, and there are several topics you can switch on instead of writing. For example, you can use dialog box themes to make your activity look like a dialog box. Defined in manifest are as follows:

<activity android:theme= "@android: Style/theme.dialog" >

If you like a theme, but want to make some minor changes, you just need to add this topic as a parent theme. 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 subject 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.

2. Set up a theme in a program

If necessary, you can load a topic in the activity by using method Settheme (). Note that if you do so, you should set the theme before initializing any view. For example, before calling 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, you need to be aware that the topic does not include any animations used by the system to start 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.

More interested readers of Android related content can view this site: "Android scroll bar and Rolling tips summary", "Android graphics and image processing skills summary", "Android Development introduction and Advanced Course", " Android Debugging tips and FAQ Solutions Summary, Android Multimedia tips summary (audio, video, audio, etc.), "Android Basic Components Usage Summary", "Android View Summary", " Android Layout layout Tips and a summary of the use of Android controls

I hope this article will help you with the Android program.

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.