一,界面与布局概述
(一)界面
应用界面包含用户可查看并与之交互的所有内容。安卓提供丰富多样的预置 ui 组件,例如结构化布局对象和 ui 控件,您可以利用这些组件为您的应用构建图形界面。安卓还提供其他界面模块,用于构建特殊界面,例如对话框、通知和菜单。
(二)
布局可定义应用中的界面结构(例如 activity 的界面结构)。布局中的所有元素均使用 view 和 viewgroup 对象的层次结构进行构建。
1.container
ui容器指viewgroup,也是view的子类,而viewgroup有几个布局子类:linearlayout、relativelayout、absolutelayout、tablelayout、gridlayout,constraintlayout。
2.ui控件 (control)
ui控件指widget(微件),不能再包含其它元素的控件,例如标签(textview)、文本框(edittext)、按钮(button)、 活动栏(action bar)、对话框(dialogs)、状态栏(status)、通知(notifications)。
在运行时实例化布局元素:应用可通过编程创建 view 对象和 viewgroup 对象(并操纵其属性)。如:
二,线性布局
在这种布局中,所有的子元素都是按照垂直或水平的顺序排列在界面上。如果是垂直排列,每个子元素占一行,如果是水平排列,则每个子元素占一列。线性布局可以支持布局样式嵌套实现复杂的布局样式
(一)常见属性
-
layout_width:布局宽度(match_parent,wrap_conent)
-
layout_height:布局高度(match_parent,wrap_conent)
-
orietation:方向(vertical,horizontal) gravity:对齐方式(left, right, center,
top, bottom…) -
background:背景(颜色、图片、选择器) weight:比重(用于瓜分手机屏幕)
-
padding:内边距 (paddingleft, paddingright, paddingtop, paddingbottom)
-
margin:外边距 (marginleft, marginright, margintop, marginbottom )
三,案例演示
1.创建安卓应用【linearlayoutdemo】
2.主布局资源文件activity_main.xml
- 将约束布局改为线性布局
- 添加两个按钮
<button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"/>
<button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"/>
- 设置线性布局的方向
- 设置内边距
- 设置线性布局的对齐方式
- 设置背景图片
- 在drawable目录里创建自定义边框配置文件customer_border.xml
"1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
android:radius="10dp" />
android:color="#04be02" />
android:width="1dp"
android:color="#555555" />
- 在``第二个按钮下添加线性布局
<linearlayout
android:layout_width="300dp"
android:layout_height="200dp"
android:background="@drawable/custom_border">
- 设置过渡色效果
- 效果如下:
案例演示 —— 线性布局嵌套
1.创建安卓应用【nestedlinearlayout】
2. 将小图片拷贝到res/drawable目录
3.布局资源文件activity_main.xml
- 将约束布局改成线性布局,设置方向属性
- 添加三个线性布局
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#ffff00"
android:gravity="center"
android:orientation="horizontal">
<imageview
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/ic_launcher_foreground"/>
<imageview
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/ic_launcher_background"/>
<imageview
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@drawable/ic_launcher_foreground"/>
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="#00ff00"
android:gravity="center"
android:orientation="vertical">
<linearlayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"/>
<button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"/>
<button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮3"/>
</linearlayout>
</linearlayout>
<linearlayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:background="#0000ff"
android:orientation="horizontal">
</linearlayout>
-
运行程序,查看结果(三个子布局按照1:2:3垂直瓜分手机屏幕)
-
在第一个布局里添加三个图像控件
本来三个子布局按照1:2:3比例垂直瓜分手机屏幕,但是在第二个子布局里添加子控件之后,瓜分比例就发生变化了,第二个子布局瓜分比例超过了第三个 -
为了保持原来的瓜分比例将三个子布局的layout_height属性值统统设置为0dp