03-Android Layout
03-Android Layout
LESSON 3
Version 1.0
Agenda
• android:weightSum="4"
• layout_weight=“1”
RelativeLayout (04)
Định vị View con bằng liên hệ giữa chúng
với nhau
<TextView
android:id="@+id/view1"
android:text="view1" android:gravity="center"
android:layout_toLeftOf="@id/view3"
android:layout_alignBottom="@id/view3"
android:layout_width="200dp"
android:layout_height="50dp"
android:background="#e8d33636" />
<TextView
android:id="@+id/view2" android:text="view2"
android:layout_alignRight="@id/view1"
android:layout_above="@id/view1"
android:gravity="center"
android:layout_width="50dp" android:layout_height="200dp"
android:background="#e71faf15" />
<TextView
android:id="@+id/view3"
android:layout_centerInParent="true"
android:layout_alignParentRight="true"
android:layout_marginRight="30dp"
android:text="view3" android:gravity="center"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#d4a00f8f" />
Exercise at class
A Guide to the Android
ConstraintLayout
• ConstraintLayout
– Introduced in Android 7
– use of this layout manager is recommended for
most layout requirements.
A Guide to the Android
ConstraintLayout (05)
• How ConstraintLayout Works
· Constraints
· Margins
· Opposing Constraints
· Constraint Bias
· Chains
· Chain Styles
A Guide to the Android
ConstraintLayout
• Constraints
Constraints are essentially sets of rules that dictate the way in
which a widget is aligned and distanced in relation to other
widgets
• Margins
A margin is a form of constraint that specifies a fixed distance
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginEnd="25dp"
/>
A Guide to the Android
ConstraintLayout
• Opposing Constraints
Two constraints operating along the same axis
on a single widget are referred to as opposing
constraints
• Attribute
Bias (phần trăm của trọng số)
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView 1"
app:layout_constraintLeft_toRightOf="@+id/textView2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView 2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/textView1"
app:layout_constraintTop_toTopOf="parent" />
A Guide to the Android
ConstraintLayout
• Constraint Bias
It has now been established that a widget in a
ConstraintLayout can potentially be subject to
opposing constraint connections
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Test Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
Chains trong Constraint layout
• Các Chain (chuỗi) cung cấp hành vi giống như
nhóm theo một trục đơn (theo chiều ngang
hoặc chiều dọc
• Để tạo một chain, ta kết nối các View với nhau
theo cả 2 hướng (bi-directional connection)
• Hai hướng ở đây có nghĩa là đuôi của view 1
neo vào đầu của view 2 và đầu của view 2
cũng cần được neo vào đuôi của view 1. ( nếu
không sẽ không tính là chain)
chainStyle của Chain
<Button
android:id="@+id/label_a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="A"
android:textSize="22sp"
app:layout_constraintEnd_toStartOf="@+id/label_b"
app:layout_constraintHorizontal_chainStyle="packed"
app:layout_constraintStart_toStartOf="parent"/>
<Button
android:id="@+id/label_b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="B"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@id/label_a"/>
Some Widgets (06)
ScrollView, ImageView
Let’s design !
Giao diện đăng ký, đăng nhập tài khoản
Exercise 3
• End of Lesson 3
Thank you!