安卓的简单心得
本人觉得在学习安卓移动开发课程的这段时间真的是太水,所以就自己去网上找了教程,并且以写博文的形式去激励自己去学好这门课。
本人学习安卓这门课用的开发工具是Android Studio。
(附上网址https://developer.android.google.cn/studio)
安装过程非常简单,网上都有教程,一般只要选择好安装路径,后面它自己就会安装好需要用到的基本内容,没有模拟器的话就自己在studio里面自己建一个。
(个人建议把东西都放一个文件夹里面,然后再去归类)
还有一个值得注意的问题是语言的选择,我自己当是在这方面就浪费了很多时间。
有的Android Studio安装好是Kotlin语言,但网上的一些源码是Java的,这个就需要在studio里面进行切换。
具体切换的方法有两种:
1、在studio里面进行修改(这种方法治标不治本,写的时候还是Kotlin语言,只是在右边多了一个java的窗口);
2、把Kotlin语言的插件去掉,这个方法可以在file->settings->plugins->installed搜索Kotlin,
点开Kotlin,在右边会有一个disable,点击就行了,Kotlin的图标就变成灰色了,没有提醒重启,那就自己重启一下。
LinearLayout
网上有很多有很多linearlayout的实例,随便搜一搜就有很多了。
我只是对我认为需要强调的内容解释一下。
<Linearlayout
Android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_height"
android:layout_weight="1"
android:text="ONE"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_height"
android:layout_weight="1"
android:text="TWO"/>
</LinearLayout>
这是一个简单的代码,既可以通过design界面直接拖拽,也可以代码直接编写。
代码编写的话需要注意的是水平分割还是垂直分割(Android:orientation的取值是horizontal和vertical的区别,对应layout_width和layout_height取值,特别是取带单位的具体数值的时候,比如1px,0dp)
layout_weight的取值代表板块的取值的比列(占剩下空间的比例),其中当height或width的取值为match_parent时,每个板块占屏幕的比例是需要计算的,具体的计算方法可以网上搜一搜。
推荐一个计算的网址:weight计算
<?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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入要保存的电话号码"/>
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal"/>
<Button
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="清空" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这是我一开始的代码,敲完就发现activity_mian.xml文件会亮起红色,但是整个工程是可以运行的,中途不会报任何的错误,而且结果也是对的。
但是这个问题怎么解决呢?
<?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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入要保存的电话号码"
app:layout_constraintStart_toStartOf="@+id/linearLayout"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/linearLayout" />
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/textView">
<Button
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="清空" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
这个是调整之后activity_main.xml文件的整个代码。
TextView、EditText、LinearLayout后都多出了app:layout_constrain***样式的几行代码。调整之后,xml文件就没有红色了,运行结果也不受影响。
这个调整不是我手动进行修改的,如果我知道的话第一次就不那么写了,那么我是怎么解决的呢?
首先我跳到design界面,在这里会看见我刚才那段代码在屏幕上的表现内容,然后点击图中的那个小魔法棒就行了。
回到text界面,activity_main.xml文件就正常了。