Yutt's Blog

Android学习笔记——用户界面

2018/01/19 Share

重要文件

app > java > com.example.myfirstapp > MainActivity.java
这是主 Activity。当构建和运行应用时,系统会启动此 Activity 的实例并加载其布局。

app > res > layout > activity_main.xml
布局文件,类似jsx。

app > manifests > AndroidManifest.xml
manifest 文件描述应用的基本特性并定义其每个组件。

Gradle Scripts > build.gradle
会看到具有此名称的两个文件:一个用于项目,一个用于“应用”模块。每个模块均有自己的 build.gradle 文件,但此项目当前仅有一个模块。

布局

编写XML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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" >
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a TextView" />
<Button android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, I am a Button" />
</LinearLayout>

属性

ID

1
android:id="@+id/my_button"

类似于html中的id,可以通过id来捕获该对象。

1
Button myButton = (Button) findViewById(R.id.my_button);

加号的意思是会在R.java文件也就是安卓的资源文件中生成响应的id号。如果id已存在,则去掉加号使用:

1
android:id="@id/my_button"

布局参数

1
2
android:layout_width="wrap_content"
android:layout_height="match_parent"
  • wrap_content 指示您的视图将其大小调整为内容所需的尺寸。
  • match_parent 指示您的视图尽可能采用其父视图组所允许的最大尺寸。

线性布局

线性布局会根据权重将屏幕分割成水平或者竖直的视图块。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical" >
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/to" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/subject" />
<EditText
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="@string/message" />
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="@string/send" />
</LinearLayout>

  • android:orientation: 指定布局方向, horizontal(水平)或vertical(竖直)
  • android:layout_weight: 权重属性,如不设置则为0。权重为0的时候,组件只占用内容所需区域。

相对布局

相对布局和web开发中的相对位置类似,可以将组件相对于父母或者兄弟元素进行相应的移动, 如果不设置相应的参数,那么组件将默认出现在父母组件的左上角。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Reminder" />
<Spinner
android:id="@+id/dates"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="@+id/times" />
<Spinner
android:id="@id/times"
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:layout_alignParentRight="true" />
<Button
android:layout_width="96dp"
android:layout_height="wrap_content"
android:layout_below="@id/times"
android:layout_alignParentRight="true"
android:text="Done"/>
</RelativeLayout>

  • android:layout_alignParentTop: true / false
  • android:layout_centerVertical: true / false
  • android:layout_below: id
  • android:layout_toRightOf: id

表格布局

类似于html中的表格,不常用。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<TableLayout 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="wrap_content">
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one"
android:layout_span="2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three"/>
</TableRow>
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three"/>
</TableRow>
</TableLayout>
  • android:collapseColumns:设置需要被隐藏的列的序号
  • android:shrinkColumns:设置允许被收缩的列的列序号
  • android:stretchColumns:设置运行被拉伸的列的列序号

以上这三个属性的列号都是从0开始算的,比如shrinkColunmns = “2”,对应的是第三列!
可以设置多个,用逗号隔开比如”0,2”,如果是所有列都生效,则用”*”号即可
除了这三个常用属性,还有两个属性,分别就是跳格子以及合并单元格,这和HTML中的Table类似:

  • android:layout_column=”2”:表示的就是跳过第二个,直接显示到第三个格子处,从1开始算的!
  • android:layout_span=”4”:表示合并4个单元格,也就说这个组件占4个单元格

网格布局

与表格布局类似。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<GridLayout 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="wrap_content"
android:rowCount="3"
android:columnCount="3"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
android:layout_gravity="right"
android:layout_columnSpan="2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"/>
</GridLayout>

CATALOG
  1. 1. 重要文件
  2. 2. 布局
    1. 2.1. 编写XML
    2. 2.2. 属性
      1. 2.2.1. ID
      2. 2.2.2. 布局参数
  3. 3. 线性布局
  4. 4. 相对布局
  5. 5. 表格布局
    1. 5.1. 网格布局