《嵌入式应用开发》实验一、开发环境搭建与布局(上)

news/2024/5/20 5:26:14 标签: android, android studio, gradle

1. 搭建开发环境

去官网(https://developer.android.google.cn/studio)下载 Android Studio

在这里插入图片描述
安装SDK(默认Android 7.0即可)

在这里插入图片描述

全局 gradle 镜像配置

在用户主目录下的 .gradle 文件夹下面新建文件 init.gradle,内容为

gradle">allprojects {
    repositories {
        def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/central'
        def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/public'
        all { ArtifactRepository repo ->
            if(repo instanceof MavenArtifactRepository){
                def url = repo.url.toString()
                if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('http://repo1.maven.org/maven2')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
                    remove repo
                }
                if (url.startsWith('https://jcenter.bintray.com/') || url.startsWith('http://jcenter.bintray.com/')) {
                    project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
                    remove repo
                }
            }
        }
        maven {
            url ALIYUN_REPOSITORY_URL
            url ALIYUN_JCENTER_URL
        }
    }


    buildscript{
        repositories {
        def ALIYUN_REPOSITORY_URL = 'https://maven.aliyun.com/repository/central'
        def ALIYUN_JCENTER_URL = 'https://maven.aliyun.com/repository/public'
            all { ArtifactRepository repo ->
                if(repo instanceof MavenArtifactRepository){
                    def url = repo.url.toString()
                    if (url.startsWith('https://repo1.maven.org/maven2') || url.startsWith('http://repo1.maven.org/maven2')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_REPOSITORY_URL."
                        remove repo
                    }
                    if (url.startsWith('https://jcenter.bintray.com/') || url.startsWith('http://jcenter.bintray.com/')) {
                        project.logger.lifecycle "Repository ${repo.url} replaced by $ALIYUN_JCENTER_URL."
                        remove repo
                    }
                }
            }
            maven {
                url ALIYUN_REPOSITORY_URL
                url ALIYUN_JCENTER_URL
            }
        }
    }
}

安装模拟器

在这里插入图片描述

在这里插入图片描述

2. 生成APK文件

两种方式,一种是debug版本,一种是带签名的版本。

debug版本

在这里插入图片描述

带签名的版本

在这里插入图片描述
在这里插入图片描述

构建完毕后可以在 app/build/outputs/apk里找到

在这里插入图片描述
运行结果:

3. 练习线性布局

番外:如何创建一个新的 Activity?

在这里插入图片描述

在这里插入图片描述YourName 替换为你要创建的 Activity的名字,点击Finish即可。

orientation

  • vertical(垂直): 从上到下
  • horizontal(水平):从左到右

dp:设置边距单位
sp:设置文字大小单位

尽量避免将宽高设置为固定值。

练习一:试着做出如下界面
在这里插入图片描述
实现解析:将整体看作一个大的线型布局(纵向),里面塞三个横向布局。
将文本1,2放入第一个横向布局,文本3放入第二个横向布局,文本4放入第三个横向布局。

<?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=".LinearActivity"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="横向排列1" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="横向排列2" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="纵向排列1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="纵向排列2" />
    </LinearLayout>

</LinearLayout>

效果如图:
在这里插入图片描述
在此基础上,使用 marginpaddingtextSizegravitylayout_gravity修饰后的效果:

在这里插入图片描述

最终代码:

<?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=".LinearActivity"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text="横向排列1" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="30dp"
            android:text="横向排列2" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="15dp"
            android:text="纵向排列1" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:padding="10dp"
            android:text="纵向排列2" />
    </LinearLayout>

</LinearLayout>

4. 练习相对布局。

强调相对定位,以其他组件或父容器作为参照物,摆放组件的位置。

  • android:gravity 设置子组件的摆放方式。
  • android:ignoregravity 设置某个子组件不受gravity的控制。

设置组件上的属性:android:layout_aboveandroid:layout_belowandroid:layout_toLeftOfandroid:layout_toRightOf

练习一:实现三个文本对齐,以第一个文本为参照相对定位。

新建一个 Activity,起名为 RelativeActivity

在这里插入图片描述
相对布局的操作就是:首先定义一个 RelativeLayout的布局,为其一个子元素赋予属性 android:id(如:@id/text1),其他元素则可以用 android:layout_below="@id/text1"来相对定位。

<?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"
    tools:context=".RelativeActivity">
    
    <TextView
        android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文本一"
    />
    <TextView
        android:id="@+id/text2"
        android:layout_below="@id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文本二"
        />
    <TextView
        android:layout_below="@id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="文本三"
        />
</RelativeLayout>

在这里插入图片描述

5. 练习表格布局。

6. 练习网格布局。

7. 练习约束布局。

8. 练习帧布局。

是Android中最为简单的一种布局。
可以实现层叠效果(从坐标(0,0)开始)、以及拖动效果。

在这里插入图片描述

  • android:gravity 设置子组件的摆放方式。
  • android:gravity 放在组件的属性描述里设置的是文字居中。
  • android:layout_gravity 设置的是当前控件在布局中的位置。

练习:创建两个文本,设置不同的颜色和大小,实现层叠效果

<?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=".FrameActivity">

    <TextView
        android:layout_width="140dp"
        android:layout_height="140dp"
        android:background="@color/purple_700"
    />

    <TextView
        android:layout_width="80dp"
        android:layout_height="80dp"
        android:background="@color/teal_700" />

</FrameLayout>

在这里插入图片描述


http://www.niftyadmin.cn/n/103977.html

相关文章

《数据库系统概论》学习笔记——第五章:数据库完整性

教材为数据库系统概论第五版&#xff08;王珊&#xff09; 本章概念比较多&#xff0c;稍微记一下 数据库的完整性 数据库的完整性指的是数据的正确性和相容性 完整性&#xff1a;指数据是符合现实世界语义、反映当前实际状况的 相容性&#xff1a;数据库同一对象在不同关系表…

《Python机器学习》基础代码2

&#x1f442; 逝年 - 夏小虎 - 单曲 - 网易云音乐 目录 &#x1f44a;Matplotlib综合应用&#xff1a;空气质量监测数据的图形化展示 &#x1f33c;1&#xff0c;AQI时序变化特点 &#x1f33c;2&#xff0c;AQI分布特征 相关性分析 &#x1f33c;3&#xff0c;优化图形…

网页js版音频数字信号处理:H5录音+特定频率信号的特征分析和识别提取

文章目录一、网页中的音频数据源二、FFT&#xff1a;时域转频域三、信号的特征分析四、信号的识别提取附录音频数字信号处理 Audio DSP (Digital Signal Processing) 是一个复杂又专业的话题&#xff0c;本文介绍的是如何从音频中实时分析和识别出特定频率信号的一种方法&#…

观测云产品更新|新增用户访问监测自动化追踪;新增 CDN 质量分析;新增自定义查看器导航菜单等

观测云更新 用户访问监测优化 新增用户访问监测自动化追踪 用户访问监测新增自动化追踪&#xff0c;通过“浏览器插件”的实现方式&#xff0c;使用浏览器记录用户访问行为&#xff0c;创建无代码的端到端测试。更多详情可参考文档【 自动化追踪 】https://docs.guance.com/…

本机安装docker,redis并进行连接实战

1、背景 win10系统&#xff0c;想要在本机搭建一套开发环境&#xff0c;需要安装zk&#xff0c;redis等组件&#xff0c;一个个的安装显然效率太低且复杂&#xff0c;这里考虑安装docker及相关镜像 2、 docker安装 docker官网下载:https://docs.docker.com/desktop/install/…

Benchmark测试——fio——源码分析

1. main 1.1 parse_options() 解析选项&#xff0c;更新数据结构 1.1.1 fio_init_options() 1.1.2 fio_test_cconv(&def_thread.o) <cconv.c> 1.1.2.1 convert_thread_options_to_cpu() 传递options给数据结构 1.1.3 parse_cmd_line() switch语句多路选择&am…

手把手带你做一套毕业设计-征程开启

本文是《手把手带你做一套毕业设计》专栏的开篇&#xff0c;文本将会包含我们创作这个专栏的初衷&#xff0c;专栏的主体内容&#xff0c;以及我们专栏的后续规划。关于这套毕业设计的作者呢前端部分由狗哥负责&#xff0c;服务端部分则由天哥操刀。我们力求毕业生或者新手通过…

Linux 之 大数据定制篇-shell 编程

文章目录1 为什么要学习 Shell 编程2 Shell 是什么&#xff1f;3 Shell 脚本的执行方式3.1 脚本格式要求3.2 编写第一个 Shell 脚本3.3 脚本的常用执行方式4 Shell 的变量4.1 Shell 变量介绍4.2 shell 变量的定义4.3 shell 变量的定义5 设置环境变量5.1 基本语法5.2 快速入门6 …