Android学习笔记2 - HelloWorld

来源:互联网 发布:数据库云 编辑:程序博客网 时间:2024/05/23 19:15
 

1、file - new - other 打开new对话框,选择Android下的Android Project,点击next

2、如下图,点击finish

3、看一下helloworld的构造。

首先看一下HelloWordActivity.java文件,代码如下

package fred.android.test.HelloWorld;import android.app.Activity;import android.os.Bundle;public class HelloWorldActivity extends Activity {    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);    }}

这是一个HelloWorldActivity活动类,活动是最基本的Android 应用程序组件,应用程序中,一个活动通常就是一个单独的屏幕。每一个活动都被实现为一个独立的类,并且从活动基类中继承而来,活动类将会显示由视图控件组成的用户接口,并对事件做出响应。

主要的一句是setContentView(R.layout.main);这是设置当前视图,表示试用res/layout/main.xml 文件初始化当前视图。

然后看一下res/layout/main.xml 文件,这是一个视图布局文件

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    /></LinearLayout>

<LinearLayout>标签表示在这个标签中声明的元素都是从上到下线性排列。
android:layout_width:当前元素的宽带(fill_parent即和父元素一致)
android:layout_height:当前元素的高度(wrap_content指随文字改变)
android:text:当前元素要显示的文字,这里引用了"@string/hello",即res/values/strings.xml 中的 name="hello" 的string元素值。

4、运行查看效果。

启动虚拟机:Window - Android SDK and AVD Manager ,选择已建好的虚拟机,start...
(小技巧:Launch Options对话框的Scale display to real size选项可以调节虚拟机的大小,因为默认是400X800,导致笔记本768的高度不能显示全,可以设置Screen Size为8)

运行程序:Run - Run AS - Android Application

等待......可以看到虚拟机上显示如下

原创粉丝点击