目录
首先需要明白绝对布局不再使用,多使用相对布局。
简单说一下我对Android长度单位的理解:
dp 斜对角(不发生变化)
sp 斜对角(随着变化而变化) 默认sp
pt 表示一个点
px 像素
in 英尺
mm 毫米
1.布局的通用属性
- android:id
- android:layout_width
- android:layout_height
- android:layout_margin
- android:padding
- android:background
android:layout_height和android:layout_width的值可以为:
三个属性都用来适应视图的水平或垂直大小,一个以视图的内容或尺寸为基础的布局比精确地指定视图范围更加方便。
1)fill_parent
设置一个构件的布局为fill_parent将强制性地使构件扩展,以填充布局单元内尽可能多的空间。这跟Windows控件的dockstyle属性大体一致。设置一个顶部布局或控件为fill_parent将强制性让它布满整个屏幕。
2) wrap_content
根据容器内的东西决定组件的大小,比如一个按钮,按钮中的字体大,那么这个按钮就大,字体小那么相应的按钮就会小些。设置一个视图的尺寸为wrap_content将强制性地使视图扩展以显示全部内容。以TextView和ImageView控件为例,设置为wrap_content将完整显示其内部的文本和图像。布局元素将根据内容更改大小。设置一个视图的尺寸为wrap_content大体等同于设置Windows控件的Autosize属性为True。
3)match_parent
Android2.2中match_parent和fill_parent是一个意思 .两个参数意思一样,match_parent更贴切,于是从2.2开始两个词都可以用。那么如果考虑低版本的使用情况你就需要用fill_parent了
2.Linearlayout布局(线性布局)
LinearLayout 是一个视图组,用于使所有子视图在单个方向(垂直或水平)保持对齐。 您可以使用 android:orientation
属性指定布局方向,其值可为:horizontal
、vertical
分别表示垂直和水平方向。
- Orientation (方向)
- layout_weight (权重)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tool="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
tool:context=".MainActivity"
android:orientation="horizontal"
android:layout_centerVertical="true"
android:layout_alignParentBottom="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="确定"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="取消"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="复制"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="粘贴"/>
</LinearLayout>
布局权重
LinearLayout
还支持使用 android:layout_weight
属性为各个子视图分配权重。子视图可以指定权重值,然后系统会按照子视图声明的权重值的比例,将视图组中的任何剩余空间按权重比例分配给子视图。 默认权重为零。
例如,如果有三个文本字段,其中两个声明权重为 1,另一个没有赋予权重,则没有权重的第三个文本字段将不会扩展到剩余空间,并且仅占据其内容所需的区域,大小由layout_width指定。 所有三个字段定义长度后还剩余的空间,将被另外两个文本字段将以同等幅度进行均分。 如果为第三个字段提供权重 2(而不再是 0),那么相当于声明现在它比其他两个字段更为重要,因此,它将获得总剩余空间的一半,其他两个均享余下空间。
权重相等的子视图
要创建一个线性布局,让每个子视图在屏幕上都占据相同的空间量,对于垂直布局,则将每个视图的 android:layout_height
均设置为 "0dp";对于水平布局,将每个视图的android:layout_width
均设置为 "0dp"。 然后,将每个视图的 android:layout_weight
均设置为 "1"。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tool="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
tool:context=".MainActivity"
android:orientation="horizontal"
android:layout_centerVertical="true"
android:layout_alignParentBottom="true">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="确定"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="复制"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="粘贴"/>
</LinearLayout>
和上述代码区分权重值和layout_width值。
<Button>
元素中的android:layout_gravity
,表示<Button>
在包含它的父ViewGroup <LinearLayout>
中的对齐方式。
3.RelativeLayout相对布局
RelativeLayout以相对位置显示各子view的位置,每个view的位置通过相对它的兄弟view或者它的父view来进行定位。
定义View相对位置的属性为android:layout_something
,所有这类属性如下表:
属性名 | 描述 |
---|---|
android:layout_above | 在指定控件的上边 |
android:layout_alignBaseline | 同指定控件在同一基线上 |
android:layout_alignBottom | 与指定控件底部对齐 |
android:layout_alignEnd | 与指定控件的end对齐 |
android:layout_alignLeft | 与指定控件的左边对齐 |
android:layout_alignParentBottom | 值为true, 与父布局底部对齐 |
android:layout_alignParentEnd | 值为true,与父布局的end对齐 |
android:layout_alignParentLeft | 值为true, 与父布局的左边对齐 |
android:layout_alignParentRight | 值为true,与父布局的右边对齐 |
android:layout_alignParentStart | 值为true, 与父布局的start对齐 |
android:layout_alignParentTop | 值为true, 与父布局的顶部对齐 |
android:layout_alignRight | 同指定控件的右边对齐 |
android:layout_alignStart | 同指定控件的start对齐 |
android:layout_alignTop | 同指定控件的顶部对齐 |
android:layout_alignWithParentIfMissing | 设置为true时,如果layout_toLeftOf, layout_toRightOf, etc的对齐对象缺失的时候,则改为父布局。 |
android:layout_below | 在指定控件的下面 |
android:layout_centerHorizontal | 值为 true, 在父布局的水平中间对齐 |
android:layout_centerInParent | 值为 true, 在父布局的水平垂直对齐 |
android:layout_centerVertical | 值为 true, 在父布局的垂直对齐 |
android:layout_toEndOf | 在指定控件的末端 |
android:layout_toLeftOf | 在指定控件的左边 |
android:layout_toRightOf | 在指定控件的右边 |
android:layout_toStartOf | 在指定空间的始端 |
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tool="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
tool:context=".MainActivity" >
<!-- android:layout_gravity="center_vertical" 对布局整体起作用-->
<!--相对布局 orientation不起作用,只在线性布局起作用-->
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="确定"/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/bt1"
android:layout_below="@+id/bt1"
android:text="取消"/>
<Button
android:id="@+id/bt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/bt2"
android:layout_below="@+id/bt2"
android:text="复制"/>
<Button
android:id="@+id/bt4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/bt3"
android:layout_below="@+id/bt3"
android:text="粘贴"/>
<!-- 只对button控件起作用-->
</RelativeLayout>
记得看注释!!!
背景图片或背景颜色:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tool="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
tool:context=".MainActivity" >
<!-- android:layout_gravity="center_vertical" 对布局整体起作用-->
<!--相对布局 orientation不起作用,只在线性布局起作用-->
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#80FFC0CB"
android:text="bt111"/>
<Button
android:id="@+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/bt1"
android:layout_below="@+id/bt1"
android:background="#ffFFC0CB"
android:text="bt222"/>
</RelativeLayout>
图片:android:background="@mippmap/name"
颜色:android:background="@mippmap/#ffffff"
颜色和不透明度 (alpha) 值以十六进制表示法表示。任何一种颜色的值范围都是 0 到 255(00 到 ff)。对于 alpha,00 表示完全透明,ff 表示完全不透明。android:background的值的格式为”#AARRGGBB”。AA即透明度,R、G、B是红绿蓝三色。每一位均为0–F的十六位数。其中透明度的数值越大,越不透明。
其他颜色:https://www.cnblogs.com/android100/p/android-setalpha.html