安卓专题

安卓开发教程之二 建立一个简单应用

上页

  编写安卓应用程序是从一个Activity开始, 正如Struts的Action必须配置XML一样,Activity类必须在AndroidManifest.xml中注册方可使用。

打开目录下 res/layout/activity_main.xml 文件,如下图(使用的是上页的项目源码):

缺省出来的界面是可视化设计界面,我们可以通过拖放来设计,我们也可以通过XML编辑来实现设计,点按下面的activity_main.xml:

 

<RelativeLayout 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"
tools:context=".MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />

</RelativeLayout>

我们删除<TextView> 元素和改变 <RelativeLayout> 为 <LinearLayout>. 如下:

<?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" >
</LinearLayout>

 

LinearLayout 是一种视图组 view group 也就是ViewGroup子类,类似布局模板,Struts的Tiles模板,这种布局是要么横向要么纵向,是由android:orientation 属性规定。每个LinearLayout中嵌套部件出现在屏幕上的顺序与在XML中一致。(类似AngularJS的Route

android:layout_width 和 android:layout_height, 是规定大小,必须的。

因为 LinearLayout是布局模板中的一个根视图,它应当填满整个屏幕,那么应用里设置的宽和高必须能够支撑这种填满。

 

增加一个文本字段

  增加 <EditText> 到<LinearLayout>.里面:

<EditText android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/edit_message" />

如同其他 View 对象一样,必须设置一些属性:

android:id

提供了视图的一个标识,从代码中可以直接使用这个Id来引用这个视图,如读取和操作。符号(@)是需要的,表示是一个XML中定义的资源,后面跟着是表达资源类型(这里是id,id是一种标识类型),然后后面跟着斜线/,斜线后面是该资源名称(edit_message)。

资源类型前的加号(+)也是必要的,第一次定义一个资源ID时使用。当你编译应用程序,SDK工具使用ID名称在项目gen/ R目录下创建一个新的资源ID,指向这个资源。一旦资源ID被第一次以这种方式声明后,其他指向这个ID的资源就不需要加号。指定一个新的资源ID,而不是需要具体的资源,如字符串或布局是使用加号是必要的。

android:layout_width and android:layout_height
使用 "wrap_content" 让视图足够大到填满整个视图,如果使用"match_parent",  EditText将匹配父布局LinearLayout.大小。
android:hint
在文本字段为空,这是定义默认的字符串显示。但不是使用硬编码字符串作为值,“@字符串/ edit_message”值指的是在一个单独的文件中定义的字符串资源。因为这是指一种具体的资源(不只是一个标识符),它不需要加号。但是,因为你还没有定义的字符串资源,但你会看到在第一次编译器错误。
 

 

增加字符串资源

  缺省字符串资源在res/values/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>

增加一个按钮

  在activity_main.xml布局 <EditText>中增加按钮

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send" />

wrap_content设计成使得两个的EditText和按钮构件只是大到适合其内容即可:

设置layout_weight是一种剩余权重的意思,缺省是0,可以设置为大于0任何值,这样在其他视图都沾满自己的空间后,这个视图将填满剩余的。当指定layout_weight后,必须将宽度设为0。

 <EditText
        android:layout_weight="1"
        android:layout_width="0dp"
        ... /> 效果如下:

最后布局配置如下:

<?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>

下页