Android style Layouts

Source: Internet
Author: User

First, style layout

First, let's look at the following style layout code, which is said to add an attribute inline to an XML control (only for this blog post):

<ButtonAndroid:id= "@+id/crime_date"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_marginleft= "16DP"Android:layout_marginright= "16DP"/><CheckBoxAndroid:id= "@+id/crime_solved"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_marginleft= "16DP"Android:layout_marginright= "16DP"Android:text= "@string/solved_btn"/><ButtonAndroid:id= "@+id/choose_btn"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_marginleft= "16DP"Android:layout_marginright= "16DP"Android:text= "@string/choose_name"/><ButtonAndroid:id= "@+id/contact_btn"Android:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:layout_marginleft= "16DP"Android:layout_marginright= "16DP"Android:text= "@string/contacter_name"/>

In the above XML layout code, it can be seen that the style of each button is exactly the same, adding a property to the control more than one time. If there are more controls using the same, do n repetitions of the work. Android offers a variety of styling UIs that can be used to solve repetitive work. Style resources are similar to CSS styles. Styles can also have hierarchies: child styles have the same attributes and property values as the parent style, can overwrite them, or add new properties.

Similar to the character resource, the style is defined in the <Resources> of the XML file and stored in the Res/values directory. In addition, the name of the resource file is not important, but according to the Convention, the style is usually defined in the Style.xml file. This file has been created by default in the Android project.

Style.xml file:

1 <Resources>2     <stylename= "Crime_btn_style1">3         <Itemname= "Android:layout_width">Wrap_content</Item>4         <Itemname= "Android:layout_height">Wrap_content</Item>5         <Itemname= "Android:layout_marginleft">16dp</Item>6         <Itemname= "Android:layout_marginright">16dp</Item>7     </style>8 </Resources>

The Layout/fragment_crime.xml file, which introduces the properties of the control in the form of a style-style name (only for this blog post):

1 <Button2     Android:id= "@+id/crime_date"3 style= "@style/crime_btn_style1"/>4 5 <CheckBox6     Android:id= "@+id/crime_solved"7 Android:text= "@string/solved_btn"8 style= "@style/crime_btn_style1"/>9 Ten <Button One     Android:id= "@+id/choose_btn" A Android:text= "@string/choose_name" - style= "@style/crime_btn_style1"/> -  the <Button -     Android:id= "@+id/contact_btn" - Android:text= "@string/contacter_name" - style= "@style/crime_btn_style1"/>

PS: It is important to note that inline attributes are preferable to inline properties when they conflict with the outer property, or that the in- context property is better than the outer -property approach.

Ii. Include and merge

Include uses a resource ID to introduce a file.

Layout/button_row.xml

1 <TableRow2 xmlns:android= "Http:/schemas.android.com/apk/res/android">3     <Buttonstyle= "@style/crime_btn_style1">4     <Buttonstyle= "@style/crime_btn_style1">5     <Buttonstyle= "@style/crime_btn_style1">6 </TableRow>

Layout/fragment_crime.xml

1 <include2     Android:layout_weight= "1"3 Layout= "@layout/button_row" />4 <include5     Android:layout_weight= "1"6 Layout= "@layout/button_row" />7 <include8     Android:layout_weight= "1"9 Layout= "@layout/button_row" />

The above code indicates that include will introduce the resource ID to the Button_row file content.

The ps:include tag can only refer to the layout layout file, that is, only the layouts file under the layout directory.

Android style Layouts

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.