Android XML Layout (most used options).
- Everything you see is a view.
- View types: TextView, ImageView, Button etc.
- Every view has attributes="values" syntax.
- Pixel / dot / px - smallest controllable element of the screen.
- dp / dip (density|device independent pixel) - unit of measurement based on a coordinate system so every device using dp then can convert it to the actual (physical) pixel size (default on Android device 1dp=1/160 of inch=0.15875mm)
- sp is the same as dp but used for representing text
- Make touch targets 48dp at least
General (to all views and view-groups) attributes
#hex-colors can be found on https://material.io/guidelines/style# /color.html#color-color-palette
android:background="#hex-color"
#android:layout_
#measure can be like:
# "75dp" or
# "wrap_content" - only space needed for displaying view content (i.e. text) # or
# "match_parent" - all space available inside the parent
android:layout_width="measure"
android:layout_height="measure"
#to use comments inside xml
<!-- this is comment -->
TextView
<TextViewAttribute-Values goes here
/>
Atributes-values
android:text="Hello World"
android:textColor="#hex-color"
#you can use `textSize` with dedicated font size or use
#`textAppearance` to make your app matching phone-user preferences:
# "?android:textAppearanceLarge" or "?android:textAppearanceMedium" or "android:textAppearanceSmall" android:textSize="24sp"
android:textAppearance="?android:textAppearanceMedium"
ImageView
<ImageViewAttribute-Values goes here
/>
Atributes-values
#`@drawable` is directory name for Android Studio where all images must be downloaded
# `pic` is the name of the picture in the `drawable` directory
android:src="@drawable/pic"
#how a picture must be presented on the screen:
#`center` if a picture is larger than a screen we'll see only central part of it
#`centerCrop` aligns to the center ov the view and crop (cut off) to the #actual size of the view
android:scaleType="center"
ViewGroups
ViewGroup is the view containing other views, so ViewGroup is the parent view and views inside it are children and all views inside the same ViewGroup are siblings. There are two types of the View Groups: LinearLayout and RelativeLayoutLinearLayout
LinearLayout arranges children only either vertical (children are below each other) or horizontal (children goes one by one next to each other horizontally).
<LinearLayout
#`horizontal` or `vertical`
android:orientation="vertical" >
Child-view-1
Child-view-2
....
</LinearLayout>
If you want to arrange children proportionally, basing on their weight, use android:layout_weight.
For example we have 3 views with corresponding weights: 1st - weight 1, 2nd - weight 2 and 3rd weight 3, then total measure of the parent will be 1+2+3, so parent's 100% measure will be split into 6 parts. If parent is 180dp and 180dp/6=30dp so 1 part equals 30dp, then 1st view will be 1part - 30dp, 2nd - 2 parts - 60dp and the 3rd - 3 parts = 90dp (30dp+60dp+90dp=180dp).
Examples:
- horizontal orientation all children with the same width, you have several choices but I use this one:
- For all children do:
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:layout_weight="1"
- vertical orientation all children with the same height, you have several choices but I use this one:
- For all children do:
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
RelativeLayout
In RelativeLAyout views are arranged relative to parent or siblings:
- relative to parent (I use regex to write shorter):
- default position of the child is the top left corner of the parent
- android:layout_alignParent(Top|Bottom|Left|Right)="(true|false)"
- android:layout_center(Vertical|Horizontal)="(true|false)"
- relative to sibling (I use regex to write shorter):
- to arrange relative to siblings you first need to name views using IDs: android:id="@+id/desired_name_here"
- and then use relation to this sibling with:
- android:layout_to(Left|Right)of="@id/given_name_here"
- android:layout_(above|below)="@id/given_name_here"
Padding & Margin
You can use padding or margin to add white space to the edges of the ViewGroup or View. Padding enlarges the View itself adding padding to the width and height (content of the view will remain in the centre of the View) and margin is used inside ViewGroup adding frame to the child View. But user of the app will see both padding and margin as the same thing if you don't use different background color for both ViewGroup and View.
#to add padding to all sides of a View:
android_padding="8dp"
#or specifying side (content of the View will be moved from that side to the other side if padding is not the same into opposite directions):
android_padding(Left|Right|Top|Bottom)="8dp"
No comments:
Post a Comment