Professional Documents
Culture Documents
layout hierarchy
● View class-hierarchy is standard object-
oriented class inheritance
○ For example, Button is-a TextView is-a View is-an Object
○ Superclass-subclass relationship
2
View hierarchy and screen layout
3
View hierarchy in the layout editor
4
Layout created in XML
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
... />
<TextView
... />
<Button
... />
</LinearLayout
5
Layout created in Java Activity code
LinearLayout linearL = new
LinearLayout(this);
linearL.setOrientation(LinearLayout.VE
RTICAL);
TextView myText = new TextView(this);
myText.setText("Display this text!");
6
Set width and height in Java code
Set the width and height of a view:
LinearLayout.LayoutParams
layoutParams =
new Linear.LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_CONTENT);
7
Best practices for view hierarchies
● Arrangement of view hierarchy affects app performance
● Use smallest number of simplest views possible
● Keep the hierarchy flat—limit nesting of views and view
groups
8
The layout editor
and Constraint
Layout
9
The layout editor with
ConstraintLayout
● Connect UI elements to
parent layout
● Resize and position
elements
● Align elements to others
● Adjust margins and 10
What is ConstraintLayout?
● Default layout for new Android Studio project
● ViewGroup that offers flexibility for layout
design
● Provides constraints to determine positions and
alignment of UI elements
● Constraint is a connection to another view, 11
Layout editor main toolbar
13
Autoconnect
● Enable Autoconnect
in toolbar if
disabled
● Drag element to any
part of a layout
● Autoconnect
14
ConstraintLayout handles
1. Resizing handle
2. Constraint line and handle
3. Constraint handle
4. Baseline handle
15
Align elements by baseline
1. Click the baseline
constraint button
2. Drag from baseline to
other element's baseline
16
Attributes pane
● Click the Attributes tab
● Attributes pane includes:
○ Margin controls for
positioning
○ Attributes such as
layout_width
17
Attributes pane view inspector
1. Vertical view size control specifies
layout_height
2. Horizontal view size control
specifies layout_width
3. Attributes pane close button
18
Layout_width and layout_height
layout_width and layout_height change with size controls
● match_constraint: Expands element to fill its parent
● wrap_content: Shrinks element to enclose content
● Fixed number of dp (density-independent pixels)
19
Set attributes
To view and edit all attributes for element:
1. Click Attributes tab
2. Select element in design, blueprint, or
Component Tree
3. Change most-used attributes
4. Click at top or View more attributes at 20