文章目录
安卓程序
-
创建安卓程序
-
程序的结构
-
使用安卓模拟器
-
运行安卓应用程序
-
安卓扩展
-
安卓程序project
- Module1 在手机上看到的就是每个应用程序的图标
- Module2
- …
-
程序结构
-
app
- mainifests 安卓全局文件
- androidManifest.xml
- java 逻辑文件
- res 资源文件
- layout 存储布局文件
- activity_main.xml 默认布局文件
- drawable 存放 png jpeg gif 9Patch图片文件
- mipmap 应用的启动图标
- values 保存字符串资源 尺寸资源 颜色资源 定义常用变量
- layout 存储布局文件
- mainifests 安卓全局文件
-
布局构建
- UI相关概念
- 控制UI界面
- 布局管理器
UI相关概念
UI设计:制作用户界面
View:视图 占据屏幕的一片矩形的区域
Vivw类位于android.view包中,但是使用的时候一般用其子类,在android.widget中
- android.view.View
- android.widget.TextView
View 的常用属性
android:id="@+id/user" id为user 组件的唯一表示
android:background="@minpmap/user" 把minpmap/user.png 作为背景 组件的背景 可为图片
或者是使用16进制颜色值#CCCCCC
android:padding="" 内边距 16dp 或者属性资源
paddingLeft Top Right Bottom
PaddingStart - Left
PaddingEnd - Right
ViewGroup 容器视图 用于包裹View – 布局管理器为其子类
- ViewGroup.LayoutParams
- ViewGroup.MarginLayoutParams
LayoutParams类控制布局的位置 高度和宽度
- android:layout_heigt 布局高度
- android:layout_width 布局宽度
可以使用具体数值 200dp 100px 也可以使用常量
- FILL_PARENT 与父容器相同 2.2后不用了
- MATCH_PARENT 与父容器相同 2.2版本之后可用一致
- WARP_PARENT 与自生内容相关
MarginLayoutParams类控制外边距
- android:layout_marginTop
- android:layout_marginBottom
- android:layout_marginStart >api. 17
- android:layout_marginEnd
Android UI 组件的层次结构
- ViewGroup
- View
- ViewGroup
- View
- ViewGroup
- …
- …
控制UI界面
选择控制UI界面的方法
- 使用xml文件控制 <推荐>
- 使用Java代码控制
- 使用Java+xml文件共同控制
- 开发自定义的View
xml文件控制布局
优点分离界面代码和java逻辑代码
- 在res/layout下创建xml布局文件
- 在java中使用
setContentView(R.layout.布局文件名)
显示布局文件的内容
Java代码中控制UI界面
- 创建布局管理器
- 设置布局管理器
- 创建组件
- 设置组件属性
- 配置组件事件
- 添加组件到布局管理器
混合控制
xml实现框架
java动态向框架中添加子view
子view的实例化一般是:
- 实例化
- 设置属性
- 添加LayoutParams
- 添加事件
- 将view添加到GroupView中
开发自定义View
package com.liyanfeng.haisun;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.View;
public class SunView extends View {
public float sunmapX = 0;
public float sunmapY = 0;
public SunView(Context context) {
super(context);
sunmapX = 290;
sunmapY = 130;
}
// 重写画图方法
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 实例化一个画笔
Paint paint = new Paint();
// 实例化一个位图
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.mipmap.sun);
// canvas开始画图 目标 x y 画笔 四个参数
canvas.drawBitmap(bitmap, sunmapX, sunmapY, paint);
if (bitmap.isRecycled()) {
// 强制回收图片
bitmap.recycle();
}
}
}
布局管理器
布局管理器:
用于控制组件的摆放的类
- RelativeLayout 相对布局管理
- LinearLayout 线性布局管理
- FrameLayout 真布局管理器
- TableLayout 表格布局管理器
- AbsoluteLayout 绝对布局管理器<启用>
- GridLayout 网格布局4.0+
相对布局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:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" //宽度跟随父容器
android:layout_height="match_parent" //高度跟随父容器
android:padding="16dp" // 内边距16dp
android:gravity="center" // 管理器内各个组件的摆放方式为剧中
android:ignoreGravity="@id/text1" // 设置id为text1的组件不受gravity影响
tools:context=".MainActivity">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
相对布局管理器提供内部类RelativeLayout.LayoutParams
来设置布局,控制内部组件的分布方式
,需要设置在内部组件中
- android.layout_above 相对与参照组件的位置
- android.layout_below
- android.layout_toLeftOf
- android.layout_toRightOf
- android.layout_alignParentBottom 组件与父容器低对齐
- android.layout_alignParentLeft
- android.layout_alignParentRight
- android.layout_alignParentTop
- android.layout_alignTop 组件与哪个组件的某个边界对齐
- android.layout_alignBottom
- android.layout_alignRight
- android.layout_alignLeft
- android.layout_centerHorizontal 组件位于布局管理器的那个位置 水平居中位置
- android.layout_centerInParent 中间位置
- android.layout_centVertical 垂直居中位置
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#cccccc"
android:padding="16dp"
tools:context=".MainActivity">
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="应用有新版本存在,是否更新?----------"
android:textColor="#ff00f0" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text1"
android:layout_alignEnd="@+id/text1"
android:layout_alignRight="@+id/text1"
android:text="以后再说" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/text1"
android:layout_toStartOf="@id/button2"
android:layout_toLeftOf="@id/button2"
android:text="马上跟新" />
</RelativeLayout>
线性布局管理器
- form表单界面
- 日历界面
- 列表界面
垂直排列 横向排列
orientation=“vertical” 垂直
orientation=“horizontal” 水平
垂直中 每一行只能放一个组件,且不会换行,组件一个挨着一个排满窗口后剩下的组件将不显示
水平中 每一列只能放一个组件 …
布局管理器的属性
- android.orientation
- android.gravity
子组件属性
- android:layout_weight 组件的权重 默认是0 用于分配父组件的剩余空间
例如:
子组件A80dp 子组件B120dp 屏幕 320dp默认情况下 A占用80dpB占用120dp 剩余120dp
如果设置两个子组件的weight都是1,则各占用剩余空间的50% A80+60=140dp B120+60=180dp
简陋的微信登录界面
<?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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="QQ号码/手机号码/电子邮件"
android:paddingBottom="20dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"
android:paddingBottom="20dp" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登录"
android:textColor="#ffffff"
android:background="#ff009688"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#cccccc"
android:text="登陆遇到问题?"
android:paddingTop="20dp"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
帧布局管理器
层叠显示
最先创建的在底层,最新创建的在上层,图层的方式
布局管理器属性
- android:foreground 前景图像–始终位于最上层
- android: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">
<TextView
android:layout_width="280dp"
android:layout_height="280dp"
android:layout_gravity="center"
android:text="111"
android:textColor="#FFFFFF"
android:background="#FF0000FF"
/>
<TextView
android:layout_width="230dp"
android:layout_height="230dp"
android:layout_gravity="center"
android:text="111"
android:textColor="#FFFFFF"
android:background="#FF0077FF"
/>
<TextView
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_gravity="center"
android:text="111"
android:textColor="#FFFFFF"
android:background="#FF00B4FF"
/>
</FrameLayout>
表格布局管理器
用在类表格结构 收货地址 个人中心信息
管理器属性
- android:collapseColumns="" 隐藏哪个列 参数为列索引 从0开始
- android:stretchColumns="" 拉伸哪个列 参数为列索引 从0开始
- android:shrinkColumns="" 拥挤的时候可收缩哪个列 参数为列索引 从0开始
子组件为行 和 列
- TableRow 行
- view组件放到行中为列
登陆界面
<?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"
android:stretchColumns="0,3"
tools:context=".MainActivity">
<TableRow
android:paddingTop="200dp">
<TextView />
<TextView
android:text="账 号:"
android:textSize="18sp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="邮箱/手机号"/>
</TableRow>
<TableRow>
<TextView />
<TextView
android:text="密 码:"
android:textSize="18sp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入6-16位数字或字母"/>
</TableRow>
<TableRow>
<TextView />
<Button
android:text="注 册"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF8247"
android:text="登 录"/>
</TableRow>
<TableRow
android:paddingTop="20dp">
<TextView/>
<TextView/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="忘记密码?"
android:textColor="#FF4500"
android:layout_gravity="right"/>
<TextView/>
</TableRow>
</TableLayout>
网格布局管理器
比较表格布局管理器 网格既可以跨列显示也能跨行显示,表格管理器只能跨行显示
布局管理器属性
- android:columnCount 网格最大列数
- android:orientation 组件排列方式
- android:rowCount 网格最大行数
内部类GridLayout.LayoutParams 控制子组件的分布
+android:layout_column 组件位于网格的第几列
+android:layout_columnSpan 子组件横向跨几列
+android:layout_columnWeight 子组件权重分配剩余位置
+android:layout_gravity 子组件以什么方式占据空间
+android:layout_row 子组件在网格的第几行
+android:layout_rowSpan 子组件纵向跨几行
+android:layout_rowWeigh 垂直权重分配剩余空间
注意:跨行跨列需要配合layout_gravity="fill"填充方式实现
通常布局管理器混合{嵌套}使用
原则
- 根布局管理器必须包含xmls命名空间属性
- 一个布局管理器有且只有一个根布局管理器
- 太深的嵌套会影响性能