Android布局管理器
View
相当于窗户上的玻璃,一块一块的。模块
常用属性
id
用与为主键设定唯一的标识
android:id="@id/user"
background
设计背景,可以引用图片或者颜色
图片
android:background="@mipmap/bg"
颜色
android:background="@color/red"
padding
用与设置四个方向的内边距
要设置成一样的可以直接使用这个属性,使用不一样的可以通过
paddingLeft(Start)/Top/Right(End)/Bottom来分别进行设置
其中Left和Start是一样的Right和End是一样的
ViewGroup
相当于与窗户的框架,用与控制玻璃(View)的大小和位置,继承自View类,用于控制其他组件的容器,因为其是抽象类,一般使用其子类来管理容器
布局宽高LayoutParams类
layout_height
layout_width
以上两属性可以设置常量值也可以设置自带的
FILL_PARENT与父容器相同
MATCH_PARENT与父容器相同
WRAP_CONTENT包括其自身内容(根据自身内容确定)
控制子组件外边距MarginLayoutParams类

控制UI界面
主要分为下面四种方法

1.使用xml布局文件控制(推荐)

2.完全使用Java代码
这种方法太过于复杂了解就可以了
3.混合Java和xml

布局管理器
控制组件在页面中的摆放
1.相对布局管理器 RelativeLayout
需要确定一个组件为参考点,别的组件相对于这个参考点来摆放,参考点可以是兄弟组件或者父容器
常用属性:
1.设置布局宽高layout_with layout_height=match_parent(与父容器相同)wrap_content(包裹其自身内容)布局管理器一般设置为与父容器相同
2.设置内边距dp
- paddingBottom
- paddingLeft
- paddingRight
- paddingTop
3.设置布局管理器中各子组件的摆放方式gravity
4.指定那个组件不受3组件的影响ignoreGravity
光通过属性管理组件不够,提供内部类
RelativeLayout.LayoutParams

实例
设置更新界面
设置背景->设置参考主键textview位于布局器中间->设置button组件相对位置
<?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:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@mipmap/background">
<TextView
android:id="@+id/textView1"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发现有新版本,你想现在安装吗"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="以后再说"
android:id="@+id/button2"
android:layout_below="@+id/textView1"
android:layout_alignRight="@+id/textView1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="立即更新"
android:id="@+id/button3"
android:layout_toLeftOf="@+id/button2"
android:layout_below="@+id/textView1"/>
</RelativeLayout>
2.线性布局管理器LinearLayout
主键按照垂直或者水平排列
垂直:orientation="vertical"每行只能放置一个组件
水平:orientation="horizontal"每一列只能放一个组件
设置组件的显示位置:
gravity可设置多个值,中间用分隔符|隔开如
gravity="right|button"右下角
子组件属性layout_weight
占父组件的权重,默认是0,大于零时是占用除自身大小外的一部分父组件空间,数值越大占用越多
实例
实现微信登录页面
<?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:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:hint="QQ号/微信号/电话号:" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:hint="密码:" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:textColor="@color/white"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="登陆遇到问题?"
android:paddingTop="20dp"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
实现了文本输入框下边距设置以及按钮,文字的位置上边距设置
3.帧布局管理器FrameLayout
组件一层一层显示,后面的组件会覆盖前面的组件
属性:
foreground:设置前景图像,始终位于最上层
foregroundGravity:设置前景图像的位置
实例:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:foreground="@mipmap/file"
android:foregroundGravity="right|bottom">
<TextView
android:id="@+id/tv01"
android:layout_width="280dp"
android:layout_height="280dp"
android:layout_gravity="center"
android:background="#FF0000FF"
android:text="蓝色背景的textview"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="230dp"
android:layout_height="230dp"
android:layout_gravity="center"
android:background="#FF0077FF"
android:text="天蓝色的背景textview"
android:textColor="#FFFFFF" />
<TextView
android:layout_height="180dp"
android:layout_width="180dp"
android:layout_gravity="center"
android:background="#FF00B4FF"
android:text="水蓝色的背景textview"
android:textColor="#FFFFFF"/>
</FrameLayout>

设置了三层逐渐减小的组件,且一个右下角的前景图片
4.表格布局管理器TableLayout
通过
属性:
collapseColumns:设置被隐藏的列(从0开始,隐藏多列用,隔开)
stretchColumns:设置某些列可以被拉伸(及占用所在行剩余的空间长度)
shrinkColumns:允许某列收缩(当某行组件超出内容范围允许某列收缩使得内容显现)
实例
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:stretchColumns="0,3">
<TableRow android:paddingTop="200dp">
<TextView/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="账 号:"
android:textSize="18sp"
android:gravity="center_horizontal"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="邮箱或手机号码" />
<TextView/>
</TableRow>
<TableRow>
<TextView/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密 码:"
android:textSize="18sp"
android:gravity="center_horizontal"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入6-16位数字或字母" />
<TextView/>
</TableRow>
<TableRow>
<TextView/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注 册" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登 录"
android:background="#FF8247"/>
<TextView/>
</TableRow>
<TableRow
android:paddingTop="20dp">
<TextView/>
<TextView/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忘记密码?"
android:textColor="#FF4500"
android:gravity="right"/>
<TextView/>
</TableRow>
</TableLayout>
实现了四行四列,通过设置上边距使其居中,以及首列和尾列可拉伸使其居中
5.网格布局管理器GridLayout
组件可以跨列跨行显示(表格只能跨列)
常用属性:
columnCount:指定网格的最大列数
orientation:没有为组件分配行和列的时候,组件的排列方式
rowCount:指定网格的最大行数
子组件控制方位的具体属性

实列:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnCount="6"
android:rowCount="11"
android:orientation="horizontal">
<Button android:text="时间" android:layout_row="0" android:layout_column="0" android:layout_width="50dp" android:textSize="12dp"/>
<Button android:text="第一节" android:layout_row="1" android:layout_column="0" android:layout_width="50dp" android:textSize="12dp"/>
<Button android:text="第二节" android:layout_row="2" android:layout_column="0" android:layout_width="50dp" android:textSize="12dp"/>
<Button android:text="第三节" android:layout_row="3" android:layout_column="0" android:layout_width="50dp" android:textSize="12dp"/>
<Button android:text="第四节" android:layout_row="4" android:layout_column="0" android:layout_width="50dp" android:textSize="12dp"/>
<Button android:text="第五节" android:layout_row="5" android:layout_column="0" android:layout_width="50dp" android:textSize="12dp"/>
<Button android:text="第六节" android:layout_row="6" android:layout_column="0" android:layout_width="50dp" android:textSize="12dp"/>
<Button android:text="第七节" android:layout_row="7" android:layout_column="0" android:layout_width="50dp" android:textSize="12dp"/>
<Button android:text="第八节" android:layout_row="8" android:layout_column="0" android:layout_width="50dp" android:textSize="12dp"/>
<Button android:text="第9节" android:layout_row="9" android:layout_column="0" android:layout_width="50dp" android:textSize="12dp"/>
<Button android:text="第10节" android:layout_row="10" android:layout_column="0" android:layout_width="50dp" android:textSize="12dp"/>
<Button android:layout_row="0" android:text="星期一" android:layout_width="65dp" android:textSize="12dp"/>
<Button android:layout_row="0" android:text="星期二" android:layout_width="65dp" android:textSize="12dp"/>
<Button android:layout_row="0" android:text="星期三" android:layout_width="65dp" android:textSize="12dp"/>
<Button android:layout_row="0" android:text="星期四" android:layout_width="65dp" android:textSize="12dp"/>
<Button android:layout_row="0" android:text="星期五" android:layout_width="65dp" android:textSize="12dp"/>
<Button android:text="物 联 网 中间件技术" android:layout_gravity="fill" android:layout_rowSpan="2" android:layout_column="1" android:layout_width="65dp"></Button>
<Button android:text="无 线传感器网络及应用" android:layout_gravity="fill" android:layout_row="5" android:layout_rowSpan="2" android:layout_column="1" android:layout_width="65dp"></Button>
<Button android:text="Android应 用 技 术" android:layout_gravity="fill" android:layout_row="1" android:layout_rowSpan="2" android:layout_column="3" android:layout_width="65dp"></Button>
<Button android:text="分 布 式 系 统@s" android:layout_gravity="fill" android:layout_row="3" android:layout_rowSpan="2" android:layout_column="3" android:layout_width="65dp" ></Button>
<Button android:text="Web应用技术 " android:layout_gravity="fill" android:layout_row="1" android:layout_rowSpan="2" android:layout_column="4" android:layout_width="65dp"></Button>
<Button android:text="数学建模 " android:layout_gravity="fill" android:layout_row="3" android:layout_rowSpan="2" android:layout_column="4" android:layout_width="65dp"></Button>
<Button android:text="Android应 用 技 术 " android:layout_gravity="fill" android:layout_row="5" android:layout_rowSpan="2" android:layout_column="4" android:layout_width="65dp"></Button>
<Button android:text="形式与政策 " android:layout_gravity="fill" android:layout_row="7" android:layout_rowSpan="2" android:layout_column="4" android:layout_width="65dp"></Button>
<Button android:text="信息安全技术" android:layout_gravity="fill" android:layout_row="3" android:layout_rowSpan="2" android:layout_column="5" android:layout_width="65dp"></Button>
<Button android:text="Web应用技术" android:layout_gravity="fill" android:layout_row="5" android:layout_rowSpan="2" android:layout_column="5" android:layout_width="65dp"></Button>
</GridLayout>
课程表的实现,调整了时间模块课程模块的大小和位置
布局管理器嵌套
同时使用多个布局管理器(一个布局管理器(根)内部含有一个或多个布局管理器),但是尽量不要嵌套太多(嵌套太深)


浙公网安备 33010602011771号