Objective
In the previous section, we focused on the permissions explained under Linux, and today we'll look at the UI design in Android and talk about the layouts that are commonly used in Android.
There are six main layouts in Android, namely linear layout (linearlayout), relative layout (relativelayout), frame layout (framelayout), Grid layout (GridLayout) and Absolute Layout (absolutelayout) and table layout (tablelayout), in these six layouts, we used to compare linear layout and relative layout and frame layout, so in terms of layout, I'm going to focus on these three layouts, and I won't outline the rest of the layout, because it's too little. To interview Android Foundation engineers later, you only need to know the layout of the andriod in six kinds, and which six kinds is enough. The interviewer will not go into the question of whether you know how the Absolutelayout layout is used, etc.
For every Android developer, screen fitting is a headache, because there are too many Android models on the market, a wide range of screen resolution Android phones, especially there are many more wonderful equipment, so we can only do to most of the screen adaptation, There are a small number of equipment market share is not big, directly ignored. Since it is a screen fit, it is natural to work on the layout, and the most we use in the adaptation is the weight attribute (weight) in the linear layout. In this section, I will parse the linearlayout in detail, including some basic properties.
A lot of properties in the layout, if we all grasp it, it may be a little unrealistic, basically very few people to remember the layout of all the properties, the common attributes of the few, we also only talk about those more commonly used properties. Basically this layout is part of the properties you have mastered, other attributes that you can guess out of yourself.
Property classification for layouts
The properties commonly used for linear layouts are as follows:
1. layout_width (控件和布局必不可少的属性) 1. 布局的宽度:两种可选值 1. match_parent 填充父窗体(fill_parent: 这个属性也是填充父窗体的,只是早已经被废弃掉了,不推荐使用) 2. wrap_content 包裹内容 3. 自定义宽度。比如我定义高度为20dp(宽度高度的度量单位是dp, 字体的度量单位是sp)2. layout_height (控件和布局必不可少的属性) 1. 布局的高度:两种可选值 1. match_parent 填充父窗体 2. wrap_content 包裹内容 3. 自定义高度。比如我定义高度为20dp(宽度高度的度量单位是dp, 字体的度量单位是sp)3. orientation(线性布局的方向) 1. vertical 垂直 2. horizontal 水平方向4. id(布局的标识,方便我们在代码中通过findViewById来找到这个控件)5. background: 布局的背景(一般用资源文件夹下的图片来引用,也可以直接使用十六进制的颜色来表示) 6. weight:权重(按比例分配布局中的元素所占的空间)7. margin 家族 1. margin_left: 距左边的距离 2. margin_right: 距右边的距离 3. margin_top: 距上边的距离 4. margin_bottom: 距下边的距离8. padding 家族: 和margin一样,只不过margin是外边距,而padding是内边距9. gravity: 文字在控件中的位置10. layout_gravity: 控件在布局中的位置
Attribute details simple explanation of the property is not what the meaning, we combine an example to see the specific use of linearlayout.
- First we look at a QQ login picture
- This is obviously a vertical linear layout, and then we'll implement this layout.
-
The source code is as follows
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" match_parent "android:layout_height=" Match_pa Rent "> <linearlayout android:layout_marginright=" 20dp "android:layout_marginleft=" 20DP "Android oid:layout_margintop= "20DP" android:orientation= "horizontal" android:layout_width= "Match_parent" Android oid:layout_height= "40DP" > <textview android:layout_width= "wrap_content" Android:layout_ height= "wrap_content" android:text= "User name:"/> <edittext android:layout_weight= "1" Android:layout_width= "0DP" android:hint= "Please enter user name" android:layout_height= "Wrap_content"/> &L t;/linearlayout> <linearlayout android:layout_margintop= "20DP" android:layout_marginright= "20DP" Android:layout_marginleft= "20dP "android:orientation=" horizontal "android:layout_width=" Match_parent "android:layout_height=" 40DP "& Gt <textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" and roid:text= "Password:"/> <edittext android:layout_weight= "1" android:layout_width= "0DP" android:hint= "Please enter password" android:layout_height= "wrap_content"/> </LinearLayout> <button android:layout_marginright= "20DP" android:layout_marginleft= "20DP" android:layout_margintop= "20DP" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "Login"/></Linear Layout>
-
Run as follows
There's a forgotten password and a password. Two controls I didn't write it, I did it on purpose, and I think it's smart. You must also know how to add these two controls to our login screen.
Weight Weight Property
- This property is one of the most common properties we use when using linear layouts, and it's about allocating screen sizes proportionally. If we use the weight property, we have to specify the width or height of the control as 0. So how do you divide it? In a word, if the control is evenly divided horizontally, the width is specified as 0, and if the control is split vertically, the height is specified as 0
-
Basic usage of weights
as follows: (The effect is not good, forgive me have not found on the Mac on the more useful software)
The implementation code is as follows:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http/ Schemas.android.com/tools "android:id=" @+id/activity_main "android:orientation=" Horizontal "android:layout_width= "Match_parent" android:layout_height= "match_parent" tools:context= "com.selfassu.layoutdemo.MainActivity" > < TextView android:background= "#f00" android:layout_weight= "1" android:layout_width= "0DP" Androi d:layout_height= "Match_parent" android:text= "Hello world!" /> <textview android:background= "#0f0" android:layout_weight= "1" android:layout_width= "0DP" android:layout_height= "Match_parent" android:text= "Hello world!" /></linearlayout>
In fact, the implementation is very simple, careful little partner must have seen, I in the following two textview to each of them are set weight equals 1, this is how to understand it? Two weights and 2, then the first textview accounted for one, the other textview accounted for a copy. A total of two, they have no one point, so they split up, if I want the first red TextView accounted for two, green TextView accounted for a copy, then how to do? We just need to change the weight value of the first TextView to 2 and the other one to 1. So red accounted for the total (3) of 2/3, green accounted for the total of 1/3, so they are 2:1 of the ratio, is not particularly simple? The rest will depend on your own to find out, learn to use the weight of proficiency, on our future to do screen matching but there is a great help yo!
Summarize
In this section we have learned a lot about the use of linear layouts, and the rest depends on your own study. Do you think the linear layout is simple? In fact, each layout is very simple. Well, in the next section, let's talk about the use of relative layouts!
LinearLayout layout of Android Starter Development (vii)