Android UI Chapter self-writing interface

Source: Internet
Author: User

For some simpler interface, write it yourself.

Here you need to understand some of the basic properties of XML files and the knowledge of Android phones.

First, the current mobile phone screen pixel density basically has 5 kinds of situations. (The following pixel density is referred to as density)

Density ldpi mdpi hdpi xhdpi xxhdpi

Density value 120 160 240 320 480

Resolution 240x320 320x480 480x800 720x1280 1080x1920

Conversion 1dp=0.75px 1dp=1px 1dp=1.5px 1dp=2px 1dp=3px

When the Android font size is "normal", the SP==DP is converted to PX and DP.

The screen widths of 1.320x480 and 480x800 are both 320dp,720x1280 and 1080x1920 screen widths are 360DP.

2. The default 360DP screen layout (more mainstream), the 320DP screen layout, layout files placed in the LAYOUT-W320DP folder, other special resolutions can be placed in the resolution of the layout files in the folder, such as layout-854x480.

Basic attributes of XML and cases:

In the process of learning the controls, we should have a more in-depth understanding of some common XML attributes, which will help us continue to learn advanced Android technology.

1:android:id

If you want to reference a control in code or in an XML layout file, the control must set the properties of the Android:id [email protected]/value or @+id/value.

The value in the first format must exist in the R.id class, that is, to specify an int type variable that already exists in the R.id class as its ID value for the current control, and if the variable does not exist, the XML layout file cannot be validated. The second is a plus sign between @ and ID, indicating that if the variable specified by value exists in the R.id class, the value of the variable that already exists is used as its ID value, otherwise a variable of type int will be created in the R.id class and stored.

2:android:layout_width and Android:layout_height

These two properties are required to set the width and height of the control, respectively. The values you can set, in addition to Fill_parent and wrap_content, can also be set to an exact value in PX or DP (DIP). Where fill_parent means to fill the space of the parent control as much as possible, if the current control is topmost, is filled with the entire screen. Wrap_content is determined based on the contents of the control, that is, setting the width and height to a minimum if the content of the control is fully displayed.

3:android:layout_margin

You can use the Android:layout_margin property to set the distance from the control to the adjacent control or edge. This property sets the distance in 4 directions, that is, up and down these 4.

For example: android:layout_margin= "50DP". If you want to set the distance in one direction individually, you can use the 4 properties of Android:margintop, Bottom, left, and right alone.

If both Android:layout_margin and these 4 attributes are set, the system takes precedence over the Android:layout_margin property values.

4:android:padding

The Android:padding property allows you to set the distance of the control's content in 4 directions from the edge of the control. Similar to Android:layout_margin, it also has the property of setting a certain direction distance individually,

It also takes precedence over these 4 individual attributes. We beginners are more likely to confuse android:padding and Android:layout_margin concepts. In fact, the two are very easy to distinguish, the layout of the property is the charge and the control or the distance between the edge, so add layout, it and the layout of the control is related to, and without layout of the property is the control of the content distance from the edge of the control, regardless of the layout position of the control.

5:android:layout_weight

This property is useful for balanced layout. The so-called balanced layout, refers to two or more controls to occupy equal proportions of the area, the proportion of which does not vary depending on the direction of the screen, the density of the screen and the total width or height changes. For example, there are 3 buttons that require a length of 1/3 in the horizontal direction, so you can set the Android:layout_weight property of these 3 controls to equal 3 values (must be a positive integer, no units required), such as android:layout_weight= "1 "。

6:android:layout_gravity and Android:gravity

These two properties represent the position of the control and the contents of the control, respectively. For example, in a <LinearLayout>> tag, there is a <Button> button that, if you want the button to be centered horizontally, you can set the Android:layout_ in the <Button> tab Gravity property, you can also set the Android:gravity property in the <LinearLayout> tab.

Android:layout_gravity and android:gravity can be set in exactly the same way as property values, commonly used property values are as follows:

Center_horizontal: Horizontal Center

Center_vertical: Vertical Centering

Center: both horizontal and vertical

Left: set to the

Right: set to the left

Top: Set to Top

Botton: Set Bottom

The above properties can be set multiple, using the "| "Number connection.

7:android:visibility

This property determines whether the current control is visible. The following 3 property values can be set.

Visible: Control Visibility

Invisible: Control is not visible, but retains control's position

Gone: Control is not visible, and the control's position is not preserved

Code modification: view.setvisibility (view.gone);

8:android:background

This property is used to set the background color or background image of the control. If you want to set the background color, you need to use a property value in the form of #color, where color represents a 16-binary colour value.

To set the background map, the format is @drawable/resourceid, where resourceId represents the resource ID of the image, and if we want to change the background of our entire interface,

You can find the top layout first, and set its android:background= "@drawable/background" in its label.

9:android:onclick

Starting with Android1.6, the view has one more Android:onclick property that allows you to directly define the method name of the Click event in the Activity class of the current layout file.

The definition of the method is consistent with the Click event, and the method name can be specified arbitrarily, but the access modifier must be public.

There are also android:clickable and android:longclickable, both of which require a Boolean-type property value that indicates whether the current control can accept a click event, which indicates whether the current control can accept a long press-click event (holding the control motionless, Trigger the long-click event).

10:android:focusable and Android:focusableintouchmode

Most controls can have the focus, that is, in the selected state. Android:focusable determines whether the current control can gain focus.

If you set the Android:foucsableintouchmode property value to True, when you touch the control, the focus is moved to the touch control, and then you need to touch the control again to respond to the Click event. Use this property to set the Android:focusable property value to True.

Good Android Interface Design example: Online Search Minitwitter

Android UI Chapter self-writing interface

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.