Trong lập trình android LinearLayout là một loại layout rất phổ biến nó giúp developer có thể sắp xếp các View theo hàng hoặc theo cột
Để làm rõ thành phần này mình sẽ thiết kế giao diện demo với cả LinearLayout theo hàng và theo.
Bước 1: Mở file res->values  ->strings.xml
1
2
3
4
5
6
<resources>
<string name="app_name">LinearLayout</string>
 <string name="textbt1">Button 1</string>
</resources>
Bước 2: tạo file res-> values  ->color.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="bg_edit">#FDFDFD</color>
 <color name="red">#FF0000</color>
 <color name="grey">#808080</color>
 <color name="white">#FFFFFF</color>
 <color name="black">#000000</color>
 <color name="yellow">#FFFF00</color>
 <color name="lightgreen">#66FF33</color>
 <color name="yellow1">#F9E60E</color>
 <color name="yellow2">#F9F89D</color>
 <color name="orange4">#F7BE45</color>
 <color name="orange5">#F7D896</color>
 <color name="blue">#0000FF</color>
 <color name="blue2">#19FCDA</color>
 <color name="blue25">#D9F7F2</color>
 <color name="grey05">#ACA899</color>
 <color name="white1">#FFFFFF</color>
 <color name="white2">#DDDDDD</color>
 <color name="skyblue4">#41627E</color>
 <color name="headercolor">#7e8db6</color>
 <color name="brown">#802A2A</color>
 <color name="rowbackground">#5EFB6E</color>
 <color name="logocolor">#08689F</color>
 <color name="logocolor_light">#619FCF</color>
 <color name="roundbackgroundlayout">#FFFFFF</color>
 <color name="layerstartitempressed">#000000</color>
 <color name="layerenditempressed">#808080</color>
 <color name="layerstartitem">#151B8D</color>
 <color name="layerenditem">#2B60DE</color>
</resources>
Bước 2: Mở file res-> layout ->layout_main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical" >
<!-- voi thuoc tinh layout_width, layout_height co 3 gia tri la match_parent, fill_parent, wrap_content -->
 <!-- tu API8+ match_parent tuong duong fill_parent -->
 <!-- wrap_content View se tuy chinh de hien thi vua du noi dung -->
 <!-- fill_parent, match_parent View se hien thi noi dung bang view lop cha no -->
<Button
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:text="@string/textbt1" />
<Button
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/textbt1" />
<Button
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/textbt1" />
<Button
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:text="@string/textbt1" />
 <!-- thuoc tinh layout_weight giup cac ban co the chia do rong cua View theo ty le -->
 <!-- trong vi du o duoi minh chia cac Button neu chung co do rong bang nhau thi gia tri -->
 <!-- cua thuoc tinh nay bang 1 neu muon do rong gap 2 lan button khac thi gia tri nay bang 2 -->
 <!-- hoac muon no nho bang 1 nua thi gia tri nay = 0.5 cac ban co the thu voi cac gia tri khac -->
 <!-- va quan sat de hieu hon ve thuoc tinh nay -->
<LinearLayout
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal" >
<Button
 android:layout_width="0dp"
 android:layout_height="match_parent"
 android:layout_weight="1"
 android:background="@android:color/darker_gray" />
<Button
 android:layout_width="0dp"
 android:layout_height="match_parent"
 android:layout_weight="1"
 android:background="@android:color/black" />
<Button
 android:layout_width="0dp"
 android:layout_height="match_parent"
 android:layout_weight="1"
 android:background="@color/blue" />
<Button
 android:layout_width="0dp"
 android:layout_height="match_parent"
 android:layout_weight="1"
 android:background="@color/headercolor" />
<Button
 android:layout_width="0dp"
 android:layout_height="match_parent"
 android:layout_weight="2"
 android:background="@color/orange4" />
<Button
 android:layout_width="0dp"
 android:layout_height="match_parent"
 android:layout_weight="0.5"
 android:background="@color/skyblue4" />
 </LinearLayout>
</LinearLayout>
Kết quả

0 nhận xét:

Đăng nhận xét

 
Top