目录

安卓开发四大组件

安卓的目录结构

安卓的基本控件

安卓常用布局

线性布局   LinearLayout

基本框架

基本属性

横向排列 android:orientation="horizontal"

纵向排序 android:orientation="vertical"

排布方式 gravity

分割占比layout_weight

分割线 divider

LinearLayout设置divider。

相对布局 RelativeLayout

相对布局定义

基本框架

相对于兄弟基本属性

相对于父元素

对齐方式

间隔

梅花布局

表格布局 TableLayout

基本框架

表格布局的定义

基本属性

内部控件属性

帧布局  FrameLayout

基本框架

6.2 属性

约束布局 ConstraintLayout

基本框架

基本属性

属性的使用

边距属性


安卓开发四大组件

1.活动 activity

2.服务 serice

3.广播接收器 BroadcastReceiver

4.内容提供者 Content Provider

安卓的目录结构

└─ASProjectTree   

├─.gradle 自动编译工具产生的文件

├─.idea 开发工具产生的文件   

├─app module模块,应用相关的东西在里边   

├─gradle gradle环境支持文件夹   

├─.gitignore Git源码版本管理忽略管理文件   

├─build.gradle gradle项目自动编译的配置文件  

├─gradle.properties   

├─gradlew 自动完成 gradle 环境的linux mac 脚本,配合gradle 文件夹使用   

├─gradlew.bat 自动完成 gradle 环境的windows 脚本,配合gradle 文件夹使用   ├─local.properties Android SDK NDK 环境路径配置   

└─settings.gradle gradle 项目的子项目管理文件

安卓的基本控件

Textview 文本框

    <TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:layout_marginBottom="10dp"android:text="My name is smile."android:textColor="@color/white"android:textSize="20dp" />

EditText 文本输入框

    <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="1"android:text="密码:"/><EditTextandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_weight="2"/></LinearLayout>

ImageView 图像输入框

    <ImageViewandroid:layout_width="200dp"android:layout_height="200dp"android:layout_gravity="center_horizontal"android:layout_marginTop="150dp"android:background="@color/blue"android:src="@mipmap/bf7562daf4567b416d5a72308195f6a" />

Button 按钮

    <Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:background="#FF4040"android:text="TALK TO ME"android:textColor="@color/white"tools:ignore="TextContrastCheck,TextContrastCheck" />

安卓常用布局

线性布局   LinearLayout

基本框架

<?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="horizontal"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮1"android:layout_marginTop="20dp"/>...</LinearLayout>

基本属性

属性 说明
android:id 唯一值
android:layout_height 高,wrap_content:(随内容变化,类似auto),match_parent:(同父元素一样)单元最好是:dp
android:layout_width 宽,同上
android:background 背景色
android:layout_margin 外边距
android:layout_padding 内边距
android:orientation horizontal水平排列(默认);vertical竖直排列
android:layout_weight 权重
android:gravity 排布方式

横向排列 android:orientation="horizontal"

<?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="horizontal"><Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮1"android:layout_marginTop="20dp"/><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="按钮2"android:layout_marginTop="20dp"android:layout_marginLeft="20dp"/><Buttonandroid:id="@+id/button3"android:layout_width="200dp"android:layout_height="wrap_content"android:text="按钮3"android:layout_marginTop="20dp"android:layout_marginLeft="20dp"/>
</LinearLayout>

做出来之后效果是

纵向排序 android:orientation="vertical"

<?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">
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮1"
        android:layout_marginTop="20dp"/>
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="按钮2"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"/>
    <Button
        android:id="@+id/button3"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:text="按钮3"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"/>
</LinearLayout>

做出来之后效果是

排布方式 gravity

gravity是排序方式

常量 描述
top 把view推到容器里的顶部。不会改变view的尺寸。
bottom 把view推到容器的底部。不会改变view的尺寸。
center 子view水平与竖直都居中。不会改变view的尺寸。
center_horizonta 子view水平居中。不会改变view的尺寸。
center_vertical 子view垂直居中。不会改变view的尺寸。
start 把view推到容器里最开始的地方。不会改变view的尺寸。
end 把子view放到容器的最尾部。不改变view的尺寸。
left 子view从容器的左边开始排布。不会改变view的尺寸。
right 子view从容器的右边开始排布。不会改变view的尺寸。

android:gravity 控制自己内部的子元素。

android:layout_gravity 是控制自己在父类的位置

分割占比layout_weight

    <LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="按钮"/><Buttonandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text="按钮"/></LinearLayout>

以为父类排序是横向排序,所以他们就按照父类的横向1:1

如果父类排序是纵向排序,他们就是纵向1:1

分割线 divider

在drawable目录里新建一个xml,叫做 divider_linear_1.xml 。

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"><solid android:color="#7986CB" /><sizeandroid:width="1dp"android:height="1dp" />
</shape>

LinearLayout设置divider。

<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#eaeaea"android:divider="@drawable/divider_linear_1"android:orientation="vertical"android:showDividers="middle">

相对布局 RelativeLayout

相对布局定义

相对布局就是这个相对于哪个处于哪里

基本框架

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/RelativeLayout1"    android:layout_width="match_parent"    android:layout_height="match_parent" >    <ImageView    android:id="@+id/img1"     android:layout_width="80dp"    android:layout_height="80dp"    android:layout_centerInParent="true"    android:src="@drawable/pic1"/>    ...</RelativeLayout>

相对于兄弟基本属性

属性名称 属性含义
android:layout_below="@id/aaa" 在指定View的下方
android:layout_above="@id/aaa" 在指定View的上方
android:layout_toLeftOf="@id/aaa" 在指定View的左边
android:layout_toRightOf="@id/aaa" 在指定View的右边
android:layout_alignTop="@id/aaa" 与指定View的上边界一致
android:layout_alignBottom="@id/aaa" 与指定View下边界一致
android:layout_alignLeft="@id/aaa" 与指定View的左边界一致
android:layout_alignRight="@id/aaa" 与指定View的右边界一致

相对于父元素

属性名称 属性含义
android:layout_alignParentLeft="true" 在父元素内左边
android:layout_alignParentRight="true" 在父元素内右边
android:layout_alignParentTop="true" 在父元素内顶部
android:layout_alignParentBottom="true" 在父元素内底部

对齐方式

属性名称 属性含义
android:layout_centerInParent="true" 居中布局
android:layout_centerVertical="true" 垂直居中布局
android:layout_centerHorizontal="true" 水平居中布局

间隔

属性名称 属性含义
android:layout_marginBottom="" 离某元素底边缘的距离
android:layout_marginLeft="" 离某元素左边缘的距离
android:layout_marginRight ="" 离某元素右边缘的距离
android:layout_marginTop="" 离某元素上边缘的距离
android:layout_paddingBottom="" 往内部元素底边缘填充距离
android:layout_paddingLeft="" 往内部元素左边缘填充距离
android:layout_paddingRight ="" 往内部元素右边缘填充距离
android:layout_paddingTop="" 往内部元素右边缘填充距离

梅花布局

做一个这样子的布局,代码详情是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"xmlns:android="http://schemas.android.com/apk/res/android" >
<Buttonandroid:id="@+id/button1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="中间的" /><Buttonandroid:id="@+id/button2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_above="@id/button1"android:layout_alignLeft="@id/button1"android:layout_marginBottom="20dp"android:text="上面的" /><Buttonandroid:id="@+id/button5"android:layout_marginTop="20dp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_below="@id/button1"android:layout_alignLeft="@id/button1"android:layout_marginBottom="20dp"android:text="下面的" /><Buttonandroid:layout_marginRight="20dp"android:id="@+id/button3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="左边的"android:layout_toLeftOf="@id/button1"android:layout_alignTop="@id/button1"/><Buttonandroid:layout_marginLeft="20dp"android:id="@+id/button4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="右边的"android:layout_toRightOf="@id/button1"android:layout_alignTop="@id/button1"/>
</RelativeLayout>

中间按钮是相对于父类的正中间,而左右上下是相对于中间按钮的上下左右

表格布局 TableLayout

基本框架

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><Buttonandroid:text="Button1"android:layout_height="wrap_content"android:layout_width="wrap_content"/></TableLayout>

表格布局的定义

TableLayout是线性布局LinearLayout的子类,属于线性布局的一个扩展,也就是说TableLayout本质上就是一个线性布局。

如果直接在表格布局里面添加控件的话,他会直接占据一行,这也是表格布局的特点,如果想在一行中添加多个控件的话,就需要添加<TableRow></TableRow>

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TableRow><Buttonandroid:text="Button1"android:layout_height="wrap_content"android:layout_width="wrap_content"/><Buttonandroid:text="Button2"android:layout_height="wrap_content"android:layout_width="wrap_content"/></TableRow>
</TableLayout>

基本属性

属性 解释
android:collapseColumns 设置需要被隐藏的列的序号
android:shrinkColumns 设置允许被收缩的列的列序号
android:stretchColumns 设置运行被拉伸的列的列序号

以上这三个属性的列号都是从0开始算的,比如shrinkColunmns = “2”,对应的是第三列.

内部控件属性

属性 解释
android:layout_column 该单元格在第几列显示
android:layout_span 该单元格占据列数,默认为1

帧布局  FrameLayout

基本框架

<?xml version="1.0" encoding="utf-8"?>
<FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/text_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="This is TextView"/><ImageViewandroid:id="@+id/image_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/ic_launcher"/></FrameLayout>

基本属性

6.2 属性

FrameLayout的属性很少就两个,但是在说之前我们先介绍一个东西:

前景图像:永远处于帧布局最上面,直接面对用户的图像,就是不会被覆盖的图片。

两个属性:

  • android:foreground:*设置改帧布局容器的前景图像

  • android:foregroundGravity:设置前景图像显示的位置

属性的使用

  • android:foreground: 设置 FrameLayout 的前景图像

    前景图像会置于 FrameLayout 内部所有 View 的层级之上,直接设置图片的资源 id 即可,如下:

android:foreground="@drawable/emercy"
  • android:foregroundGravity: 设置 FrameLayout 前景图片的摆放位置,设置方式与 RelativeLayout 中的 View 一样,如:

android:foregroundGravity="bottom|right"
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:foreground="@drawable/emercy"android:foregroundGravity="bottom|right"android:orientation="vertical"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:scaleType="centerCrop"android:src="@mipmap/ic_launcher" /><TextViewandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="40dp"android:background="#4C374A"android:padding="10dp"android:text="Welcome to iMooc"android:textColor="#FFFFFF"android:textSize="20sp" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="left|bottom"android:background="#1E64D8"android:padding="10dp"android:text="Emercy Android"android:textColor="#FFFFFF"android:textSize="18sp" />
</FrameLayout>

约束布局 ConstraintLayout

基本框架

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Hello World!"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>

基本属性

属性 说明
layout_constraintLeft_toLeftOf 该控件的左边与设置值id的左边对齐
layout_constraintLeft_toRightOf 该控件的左边与设置值id的右边对齐
layout_constraintRight_toLeftOf 该控件的右边与设置值id的左边对齐
layout_constraintRight_toRightOf 该控件的右边与设置值id的右边对齐
layout_constraintTop_toTopOf 该控件的上边与设置值id的上边对齐
layout_constraintTop_toBottomOf 该控件的上边与设置值id的下边对齐
layout_constraintBottom_toTopOf 该控件的底边与设置值id的上边对齐
layout_constraintBottom_toBottomOf 该控件的底边与设置值id的底边对齐
layout_constraintBaseline_toBaselineOf 控件间的文本内容基准线对齐
layout_constraintStart_toEndOf 该控件的起始边与设置值id的尾边对齐
layout_constraintStart_toStartOf 该控件的起始边与设置值id的起始边对齐
layout_constraintEnd_toStartOf 该控件的尾边与设置值id的起始边对齐
layout_constraintEnd_toEndOf 该控件的尾边与设置值id的尾边对齐

属性的使用

相对父layout的定位

将子view对齐到父layout的各个边缘位置。

约束的参考对象是 parent 。

<androidx.constraintlayout.widget.ConstraintLayoutandroid:id="@+id/c2"android:layout_width="match_parent"android:layout_height="140dp"android:layout_marginTop="20dp"android:background="#f0f0f0"app:layout_constraintTop_toBottomOf="@id/c1"><!-- 相对父layout的边缘定位 --><TextViewstyle="@style/ConSampleText"android:text="居中"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewstyle="@style/ConSampleText"android:text="左上角"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewstyle="@style/ConSampleText"android:text="左下角"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintStart_toStartOf="parent" /><TextViewstyle="@style/ConSampleText"android:text="右下角"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent" /><TextViewstyle="@style/ConSampleText"android:text="右上角"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewstyle="@style/ConSampleText"android:text="顶部水平居中"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewstyle="@style/ConSampleText"android:text="底部水平居中"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent" /><TextViewstyle="@style/ConSampleText"android:text="左边垂直居中"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewstyle="@style/ConSampleText"android:text="右边垂直居中"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

边距属性

属性 说明
android:layout_marginStart 起始边距
android:layout_marginEnd 尾边距
android:layout_marginLeft 左边距
android:layout_marginTop 上边距
android:layout_marginRight 右边距
android:layout_marginBottom 底边距

Android的基础使用相关推荐

  1. 2017-2018-2 20165236 实验四《Android开发基础》实验报告

    2017-2018-2 20165236 实验四<Android开发基础>实验报告 一.实验报告封面 课程:Java程序设计       班级:1652班       姓名:郭金涛     ...

  2. Android NDK基础样例

    Android NDK基础样例 NDK(Native Development Kit),用C/C++封装一些东西?好像就这么理解好了== 一.环境准备 这个好讨厌==!因为我环境都已经搭了很久了. 已 ...

  3. Android零基础入门第11节:简单几步带你飞,运行Android Studio工程

    2019独角兽企业重金招聘Python工程师标准>>> 之前讲过Eclipse环境下的Android虚拟设备的创建和使用,现在既然升级了Android Studio开发工具,那么对应 ...

  4. Android零基础入门第30节:两分钟掌握FrameLayout帧布局

    原文:Android零基础入门第30节:两分钟掌握FrameLayout帧布局 前面学习了线性布局.相对布局.表格布局,那么本期来学习第四种布局--FrameLayout帧布局. 一.认识FrameL ...

  5. Android零基础入门第25节:最简单最常用的LinearLayout线性布局

    原文:Android零基础入门第25节:最简单最常用的LinearLayout线性布局 良好的布局设计对于UI界面至关重要,在前面也简单介绍过,目前Android中的布局主要有6种,创建的布局文件默认 ...

  6. qtdesigner怎么实现菜单栏跳转_人人都可写代码-Android零基础编程-app 入口菜单栏实操08...

    欢迎来到人人都可写代码,大家好,我是杨晓华,今天我们的课程内容是项目实操,以智者精选项目为例,编写一个Android app的入口关联菜单栏逻辑. 这是要实现的菜单栏组件展示效果,下面就是教大家如何制 ...

  7. Android零基础入门第44节:ListView数据动态更新

    2019独角兽企业重金招聘Python工程师标准>>> 经过前面几期的学习,关于ListView的一些基本用法大概学的差不多了,但是你可能发现了,所有ListView里面要填充的数据 ...

  8. Android零基础入门第38节:初识Adapter

    2019独角兽企业重金招聘Python工程师标准>>> 在上一节一起了解了ListView的简单使用,那么本节继续来学习与ListView有着千丝万缕的Adapter. 一.了解MV ...

  9. Android零基础入门第77节:Activity任务栈和启动模式

    2019独角兽企业重金招聘Python工程师标准>>> 通过前面的学习,Activity的基本使用都已掌握,接下来一起来学习更高级的一些内容. Android采用任务栈(Task)的 ...

  10. Android零基础入门第65节:RecyclerView分割线开发技巧

    2019独角兽企业重金招聘Python工程师标准>>> 在上一期通过简单学习,已经领略到了RecyclerView的灵活性,当然都是一些最基础的用法,那么本期一起来学习Recycle ...

最新文章

  1. 《编译与反编译技术实战 》一2.3 编译器的设计与实现概述
  2. 百度大脑发挥AI“头雁效应” 王海峰:在AI时代共同推动社会智能化升级
  3. metasploit 中 exploit模块check方法简介
  4. 实现一个队列类,该类用两个栈来实现
  5. Cloud for Customer的前端如何判断自己是运行在PC浏览器还是移动设备里
  6. centos 编译mysql5.6_centos下编译安装MySQL5.6
  7. springmvc是什么_SpringBoot与SpringMVC的区别是什么?
  8. sql server 面试_SQL Server复制面试问题与解答
  9. 代码编辑器[0] - Vim/gVim[1] - Vim 的快捷键操作
  10. java 处理txt_java 读写 txt 文件
  11. Java修改图片格式
  12. 【Shashlik.EventBus】.NET 事件总线,分布式事务最终一致性简介
  13. 为什么要报考系统架构设计师考试
  14. 基层群众工作存在的问题_联系服务群众方面存在的问题及原因
  15. Spring+Mybatis整合
  16. 给大家推荐一款好用的背单词APP(目前只支持苹果手机)
  17. 计算机网络 | 实验二 WINPCWP编程
  18. 华为机试字符串分割c语言,2014年华为上机机试c语言标题和答案
  19. Vertu模仿秀遭热捧 折射黑手机泛滥主因
  20. VC++6.0报错: include stdafx.h before including this file for PCH

热门文章

  1. html 语音识别输入法,从游戏语音输入说起:语音识别如何引领输入法变局
  2. (最小割)洛谷P1361 小M的作物
  3. 找出360云盘的离线下载
  4. 单比特纠错海明码校验位的计算过程
  5. 互联网架构:从设计到开发让你少踩坑
  6. Burpsuite抓包工具傻瓜式安装,安装既是中文
  7. POJ1151 Atlantis(线段树,扫描线,离散化,矩形面积并)
  8. excel无法复制粘贴已保护的工作表
  9. 命令行运行testNG
  10. 在线制作版画式logo教程