视频地址:https://ke.qq.com/course/389280?taid=3328286122176672
Android项目目录
├── HelloIxuea.iml //编辑器的配置文件
├── app //一个模块(Module),类似Eclipse中的项目
│ ├── app.iml //模块配置文件
│ ├── build //这个模块编译相关的文件
│ ├── build.gradle //这个模块的编译配置文件
│ ├── libs //依赖相关,jar
│ ├── proguard-rules.pro //混淆文件
│ └── src
│ ├── androidTest //android测试Java源码
│ │ └── java
│ │ └── com
│ │ └── ixuea
│ │ └── courses
│ │ └── helloixuea
│ │ └── ExampleInstrumentedTest.java
│ ├── main //源码,配置和资源文件
│ │ ├── AndroidManifest.xml #清单文件,用来配置当前这个项目的一些信息
│ │ ├── java #源码
│ │ │ └── com
│ │ │ └── ixuea
│ │ │ └── courses
│ │ │ └── helloixuea
│ │ │ └── MainActivity.java
│ │ └── res //资源,包括图片,声音,视频等资源
│ │ ├── drawable
│ │ │ └── ic_launcher_background.xml
│ │ ├── drawable-v24
│ │ │ └── ic_launcher_foreground.xml
│ │ ├── layout
│ │ │ └── activity_main.xml
│ │ ├── mipmap-anydpi-v26
│ │ │ ├── ic_launcher.xml
│ │ │ └── ic_launcher_round.xml
│ │ ├── mipmap-hdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-mdpi //后面的mdpi表示手机的分辨率
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ ├── mipmap-xxxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
│ └── test //java测试代码
│ └── java
│ └── com
│ └── ixuea
│ └── courses
│ └── helloixuea
│ └── ExampleUnitTest.java
├── gradle //当前项目的配置文件
│ └── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradle.properties //gradle这个编译工具的配置文件
├── gradlew //对gradle命令的一个Unix系统包装
├── gradlew.bat //对gradle命令的一个Windows系统包装
├── local.properties //本地的配置文件
└── settings.gradle //当前项目的设置文件
Activity的一般结构
逻辑层面
一个Java类:用来描述逻辑,例如:按钮点击;登录这样的逻辑。
一个布局:用来描述样式和位置,例如:按钮颜色,按钮大小,按钮的位置。
Activity组成部分
一个Activity对应一个Java类文件和一个XML布局文件,他们的分工是Java类用来实现逻辑,比如:按钮点击了,请求网络,XML用来布局,比如:界面上到底是显示一个按钮还是显示文本控件。
默认创建的Java文件:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
默认创建的XML布局文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="com.ixues.courses.helloworld.MainActivity"
<TextView
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" />
</android.support.constraint.ConstraintLayout>
也就是说我们修改XML就可以更改显示IDE内容,比如:我们将上面XML中的TextView控件的text属性修改为Hello 爱学啊,然后在运行到手机你就会看到显示的是Hello 爱学啊!。
Android控件分类
Android中,控件(Widget)可以分两大类。
** 容器控件 **
特点是,他的内部还能放其他控件,例如:LinearLayout。
** 非容器控件 **
特点是,他们的内部不能放其他控件,例如:Button。
Android中的尺寸单位
在生活中,对于长度,有米,厘米等长度单位;在平面设计中有px单位;而在Android中也有px单位,但不推荐使用;推荐字体使用sp单位,除字体外都使用dp单位。
什么是sp,dp单位呢?
dp(Device independent pixel,Density independent pixel,DIP,DP,又叫与设备无关像素或与密度无关像素或密度独立像素);简单来讲就是他会在不同的设备上大小不一样;根据屏幕的缩放自动计算;缩放在Android中相应的API获取;这就是为什么同样都是13,14寸笔记本,普通Windows笔记本和Macbook Pro比较,明显感觉Macbook Pro屏幕清晰;就是因为Macbook Pro是Retina屏幕(也称高分辨率屏幕),他的分辨率宽度为2560;而大部分14寸Windows笔记本还是1366分辨率;而Macbook Pro的缩放就是2,所以说如果使用dp单位,1dp在缩放为1的设备上,就是1dp,在缩放为2的设备上就是2px。
所以如果你某天见到谁,在Macbook上装了一个Windows,可能并不是别人装X,而是喜欢Macbook分辨率;因为通常Windows 2k分辨率笔记本都很贵,而且很少。
sp:有dp的功能;他还会根据用户的字体大小偏好来做缩放。
线性布局LinearLayout的使用
前面我们看到TextView控件,他是在屏幕中心,这是如何实现的呢?
其实是他外部的ConstraintLayout实现的,他是约束布局,入门课程中不讲解,我们现在要讲解LinearLayout,因为我们后面还会讲解很多知识点,所以我们希望每个知识点都对应一个Button,点击后会跳转到单独的界面,这样大家不论是学习,还是复习就很方便;可以翻译为“线性布局”,他可以实现水平,垂直列表;我们这里用他实现水平列表。
如何使用
很简单,只需要把ConstraintLayout换成LinearLayout,去除TextView上一些与ConstrainLayout相关的属性,不去除也不会报错,但留着也没什么效果,所以去掉:
<?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">
<TextView
android:id="@+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="20sp"
android:textColor="#f90"
android:background="#09f" />
</LinearLayout>
现在TextView就显示在左上角了。
实现垂直列表
LinearLayout默认的方向是水平(horizontal);更改为垂直只需要设置orientation属性为vertical。例如:添加两个TextView,他们在垂直方向排列:
<?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"
tools:context=".MainActivity">
<TextView
android:id="@+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="20sp"
android:textColor="#f90"
android:background="#09f" />
<TextView
android:id="@+id/text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:textSize="20sp"
android:textColor="#f90"
android:background="#09f" />
</LinearLayout>
边距
可以看到现在TextView太靠近屏幕,可以给LinearLayout设置padding,也可以给TextView设置margin;例如:给LinearLayout设置padding为10dp,这样TextView到LinearLayout就有10dp的距离:
<?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="20dp"
tools:context=".MainActivity">
···
</LinearLayout>
其中“···”表示原来的内容省略,因为大纲上没有,并且这里没有更改它,如果再完整贴上来,内容太多了。
如何使用Button按钮
Button(按钮)
用于用户触发点击事件,比如:登录。
设置Button文字,文字大小和颜色和TextView差不多,也是使用text,textColor,textSize。
<!-- 添加一个Button -->
<Button
android:textColor="#f00"
android:textSize="22sp"
android:text="你敢点击我吗?"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Button捕获点击事件
点击事件的实现,第一步也是要找到控件,然后给他设置一个监听器(当然还有其他方式实现,后面讲解),一般情况下监听器是一个接口,当然也可以是抽象类。首先在不居中添加一个Button,也就是按钮。
<Button
android:id="@+id/bt"
android:textColor="#f00"
android:textSize="22sp"
android:text="你敢点击我吗?"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Activity:
//在这里找到Button
Button bt=findViewById(R.id.bt);
//设置点击监听器,这一点和Java中的GUI编程差不多
bt.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
//点击按钮后,他就会执行这个方法
//基本上所有的Android事件都是这样一个使用方法
//显示一个提示,就是Android特有的吐司
Toast.makeText(MainActivity.this,"你还真点击啊!",Toast.LENGTH_SHORT).show();
}
});
现在在运行到手机,可以看到有一个Button,点击Button还会弹出一个提示,到这里大家基本上就学会了Android开发的基本流程,接下来就需要学习Android中其他控件,比如:如何显示一个文本输入框。
Android Studio可视化布局
可以看到前面,我们都是直接编辑XML,其实Android Studio也可以可视化方式布局,但Android开发一般还是倾向直接编辑XML,可能是最早Android开发编辑器可视化太差了,所以大家都喜欢直接编辑XML,不过现在可视化编辑器稳定多了,所以建议大家还是两种都学下,所以我们这里讲解下;而iOS开发就不一样,多数情况下使用可视化,因为storyboard虽然也是XML,但特别负载,不适合直接编辑XML;就像Macbook的触控板比鼠标好用,只要学会了,完全可以不用鼠标;但大部分Windows笔记本触摸板简直是没法用(当然我也没用过什么贵的Windows基本,只是拿7~8千的笔记本说)。