- 上次做了一个叫Just Java的点咖啡用的App
- 嵌套布局(这次要讲嵌套布局),是这样的:
1.1 代码如下:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="wang.relish.ordercoffee.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:text="quantity"
android:textAllCaps="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:onClick="decrease"
android:text="-" />
<TextView
android:id="@+id/tvQuantity"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="16dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:gravity="center_vertical"
android:text="0"
android:textSize="24sp" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:onClick="increase"
android:text="+" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:text="Price"
android:textAllCaps="true" />
<TextView
android:id="@+id/tvPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:text="$0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:onClick="submitOrder"
android:text="Order" />
</LinearLayout>
1.2 布局结构如下:
1.3 如何阅读xml布局文件,并想象出它的模样:
1.4 推荐间距:上下为16dp,左右为8dp。
1.5 常见错误:
1.5.1 变量需先声明再使用。
1.5.1.1 未声明的变量不能被赋值
1.5.1.2 未声明的变量不能被调用
1.5.2 变量声明规范:小驼峰命名法(首字母小写,其他单词首字母大写)
1.5.3 转义字符:
\a :响铃(BEL)
\b :退格(BS) ,将当前位置移到前一列
\f :换页(FF),将当前位置移到下页开头
\n :换行(LF) ,将当前位置移到下一行开头
\r :回车(CR) ,将当前位置移到本行开头
\t :水平制表(HT) (跳到下一个TAB位置)
\v :垂直制表(VT)
\ :代表一个反斜线字符”\’
\’ :代表一个单引号(撇号)字符
\” :代表一个双引号字符
\? :代表一个问号
\0 :空字符(NULL)
\ddd :1到3位八进制数所代表的任意字符
\xhh :1到2位十六进制所代表的任意字符
1.5.4 字符串连接:
1.5.4.1 连接符:“+”
1.5.4.2 变量与常量连接:
int num = 3;
System.out.println(“The Number is “ + num);
1.5.4.3 字符串连接时注意分割:”\n”或” ”