Android View Layout

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来填充数据。

ArrayAdapter

SimpleCursorAdapter

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

学知识拯救世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值