View
about layouts
声明layout方式
xml中声明
程序动态声明
常见属性
id
Any View object can have an integer ID associated with it to uniquely identify the View within the tree.
An ID doesn’t need to be unique throughout the entire tree, but it must be unique within the part of the tree you search. It might often be the entire tree, so it’s best to make it unique when possible.
Layout parameters
定义layout parameters作用于Layout中的View或者View Group.
In general, we don’t recommend specifying a layout width and height using absolute units such as pixels. A better approach is using relative measurements, such as density-independent pixel units (dp), wrap_content, or match_parent, because it helps your app display properly across a variety of device screen sizes. The accepted measurement types are defined in Layout resource.
Layout position
每个视图都是矩形的,并且都有位置坐标与尺寸描述。
位置坐标表示的是x_y坐标系中的左上角坐标。(相对于Parent而言,单位pixel)
尺寸描述的是视图矩形的宽和高。
Size, padding, and margins
measured size 期望长宽
The first pair is known as measured width and measured height. These dimensions define how big a view wants to be within its parent. You can obtain the measured dimensions by calling getMeasuredWidth() and getMeasuredHeight().
actual size 实际长宽
The second pair is known as width and height, or sometimes drawing width and drawing height. These dimensions define the actual size of the view on screen, at drawing time and after layout. These values might, but don’t have to, differ from the measured width and height. You can obtain the width and height by calling getWidth() and getHeight().
padding 填充
margin 留白
viewGroup支持留白。
Although a view can define a padding, it doesn’t support margins. However, view groups do support margins. See ViewGroup and ViewGroup.MarginLayoutParams for more information.
<?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">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:padding="8dp"
android:text="Hello, I am a TextView" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:paddingStart="8dp"
android:paddingTop="4dp"
android:paddingEnd="8dp"
android:paddingBottom="4dp"
android:text="Hello, I am a Button" />
</LinearLayout>
Common layouts
布局越浅越好。
Build dynamic lists
可以使用RecyclerView去构建动态视图。例如不确定长度的单列视图或者网格视图。
Fill an adapter view with data
可以使用ArrayAdapter或者SimpleCursorAdapter来填充数据。