原文地址:http://android.xsoftlab.net/training/basics/firstapp/building-ui.html
在这节课中,你会创建一个xml的布局文件,它包含一个Text成员和一个Button成员。在下节课中,你会学习到当Button按下后启动并发送Text成员的内容到另一个Activity中。
安卓应用的灵活用户界面是建立在View和ViewGroup的层级基础之上。View是比如Button或者TextView这种通用的UI控件,ViewGroup是一种不可见的布局容器,它定义了子View该如何被放置,比如放置在Grid或者垂直的列表中。
布局都是ViewGroup的子类,在这个练习中,我们使用LinearLayout做演示。
<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="horizontal" >
</LinearLayout>
LinearLayout是一个ViewGroup,它是ViewGroup的子类,它可以通过属性android:orientation布置子View为垂直排列或者水平排列。每一个LinearLayout的子View都会按照在XML文件中的顺序方式显示在屏幕上。
有两个属性:android:layout_width 和 android:layout_height,对于所有的View的尺寸来说是必须的。
因为LinearLayout是布局的根目录,它应该会填充屏幕的整块区域,因为在宽和高的属性中设置了”match_parent”,这个值表明了这个View应该扩展它的宽或者高到父View的高宽。
正如每一个View对象一样,你必须在XML文件中声明EditText对象的属性。
1. 在activity_my.xml文件的< LinearLayout >的元素中,定义一个< EditText >的元素,并且设置它的id属性为@+id/edit_message。
2. 设置layout_width和layout_height属性为wrap_content。
3. 定义hint属性为一个名称为edit_message的字符串。
< EditText >元素应该是这样的:
res/layout/activity_my.xml
<EditText android:id="@+id/edit_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/edit_message" />
以下是你添加的< EditText >的属性介绍:
android:id
android:layout_width 和 android:layout_height
android:hint
Note: 这里的字符串资源命名和EditText元素的名称一样,但是,它们属于不同的资源类型,一个是string,一个是id,所以使用相同的名称并无大碍。
默认情况下,在工程的res/values/下的strings.xml文件便是字符串资源文件了。这里,你将会添加一个名为”edit_message”的字符串然后设置其值为”Enter a message.”
1. 在AS中,在res/values目录下打开string.xml
2. 添加一行名为”edit_message”值为”Enter a message.”的字符串资源
3. 添加一行名为”button_send”值为“Send”的字符串资源。
4. 移除”hello world”那一行。
那么strings.xml应该就是这样的:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">My First App</string>
<string name="edit_message">Enter a message</string>
<string name="button_send">Send</string>
<string name="action_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
在用户界面上的文字,总是要指定每一个资源。字符串资源允许你管理所有的UI文字,它可以使你更轻松的找到和更新文字。外部化存储的字符串资源允许你去简单定义不同的语言版本app
你的< LinearLayout >应该看起来像这样:
res/layout/activity_my.xml
<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="horizontal" >
<EditText android:id="@+id/edit_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:hint="@string/edit_message" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" />
</LinearLayout>
Note: 这里不需要给Button指定android:id属性,因为这里不需要在Activity代码中使用Button的引用。
现在这个布局文件显示的应该是这个样子:
这下按钮就可以工作了,但是和text不同,text稍后会被用户输入一些东西,它便会很好的填充屏幕上没有使用的空间。你可以使用LinearLayout的权重属性做到这一点,可以使用android:layout_weight来指定权重。
如果所有的weight值都是0,那么如果你在其中一个View中指定任何比0大的值,那么这个View将会把剩下的空余空间全部占满。
如果需要使EditText占据屏幕上的剩余空间,那么只需要做到以下几点:
1. 在EditText的属性中添加layout_weight,并设置值为1。
2. 另外,EditText的layout_width属性可以设置为0dp:
<EditText android:layout_weight="1" android:layout_width="0dp" ... />
3 . 当指定了权重会影响布局效果,应该把EditText的宽设置为0,因为使用了wrap_content的话会要求系统计算一次不必要的宽度,因为权重会要求系统进行重新计算并使用剩余的宽度。
以下就是使用了权重的效果:
以下是修改好的布局:
<?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="horizontal">
<EditText android:id="@+id/edit_message" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:hint="@string/edit_message" />
<Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" />
</LinearLayout>
在AS中点击toolbar上的Run按钮。
如果要使用命令行,切换路径到工程下,然后执行:
ant debug
adb install bin/MyFirstApp-debug.apk
下一节会学习如何响应button的点击事件,读取EditText的内容,然后启动另一个Activity。