Hello, My Android(二)

接下来,我们继续看属于LinearLayout的子控件的两个控件:TextView和Button。有关Android界面中可以用到的Widget有哪些我会在以后再描述。

===================

TextView的声明为:

<TextView
    android:id=@+id/lblHello
    android:layout_width=fill_parent
    android:layout_height=wrap_content
    android:text=@string/hello
    />

这里,需要注意两个地方。每个Widget需要有一个id来标识自己,同时也可以为日后的操作提供一个标志符号。这里,我采用的定义是@+id/lblHello。它表示,我定义一个id,名字是lblHello,同时如果这个id还不存在,那么就创建这个id;另外,这个Widget的text@string/hello,这表示我希望用string类型的资源中名称为hello的字符串的内容作为我这个标签的文本。这里并不提倡使用静态文本,因为这样做对将来的i18n不利。

<Button>的定义与之类似。这里不再重复说明。

===============

main.xml一旦保存后,Eclipse/Android项目会自动更新R.java文件,其中就会包含我们已经定义好的一些标识符和对应的常量。例如:

public static final class id {
    public static final int btnClickMe=0x7f050001;
    public static final int lblHello=0x7f050000;
}

同时,我们可以看看string.xml文件,看里面有些什么:

<string name=hello>Hello, My Android!</string>
<string name=app_name>你好,我的Android!</string>
<string name=btn_click_me>Click Me!</string>
<string name=str_output>You clicked me on %s for %d time(s).</string>

这里,有些字符串的定义已经出现了,有些还没有出现。不要紧,在后面的编程中,我们会看到的。

我们已经有了界面后,需要做的事情就是进行事件的响应和处理。

它的界面效果是这样的:

[![HelloMyAndroid的界面](http://www.rsywx.net/wordpress/wp-content/uploads/2008/11/hellomyandroid1-156×300.jpg “hellomyandroid”)](http://www.rsywx.net/wordpress/wp-content/uploads/2008/11/hellomyandroid11.jpg)
HelloMyAndroid的界面\

(未完待续)

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *