Android Sample Code之API Demos (Activity一): Hello World

本文从Android API Demos的Hello World Activity入手,介绍了如何使用Eclipse创建Sample Project,并详细解释了布局文件中控件的id设置、宽度配置、内容对齐方式以及文本内容引用的方法,为初学者提供了基础的Android开发知识。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这一篇我就从API Demos/Activity/Hello World开始吧。学习任何一个语言,我相信大家都是从hello world开始。

首先,我们用Eclipse打开API Demos工程,这里我只介绍android 2.2的sample code。选择File--->New--->Project...,在弹出的对话框种选择Android Sample Project, 


然后,选择android 2.2,最后选择ApiDemos,点finish,就打开了。

然后运行下看看效果吧:


我们来看看代码吧。哇!这么多,从哪看啊!不急,每个android工程都有个AndroidManiFest.xml, 我不会一句一句介绍它,我会在后面一点一点的讲解,最后等我们看完了整个Activity demo。
直接上代码了HelloWorld.java:
public class HelloWorld extends Activity
{
    /**
     * Initialization of the Activity after it is first created.  Must at least
     * call {@link android.app.Activity#setContentView setContentView()} to
     * describe what is to be displayed in the screen.
     */
    @Override
	protected void onCreate(Bundle savedInstanceState)
    {
        // Be sure to call the super class.
        super.onCreate(savedInstanceState);

        // See assets/res/any/layout/hello_world.xml for this
        // view layout definition, which is being set here as
        // the content of our screen.
        setContentView(R.layout.hello_world);
    }
}

很简单,这里有个HelloWord Activity,当这个Activity创建时会运行onCreate函数,这里必须调用父类的onCreate,你不用管的,然后调用 setContentView来显示界面,界面类容就是资源R中的layout资源中的hello_world布局,界面就在hello_world.xml文件中完成的,这就是Java中常说的MVC结构了,把逻辑和界面显示分开了,这里要强烈建议大家这么去做,不然以后要改个界面就烦死你!

看看hello_world.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2007 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<!-- Demonstrates basic application screen.
     See corresponding Java code com.android.sdk.app.HelloWorld.java. -->

<!-- This screen consists of a single text field that
     displays our "Hello, World!" text. -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/text"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:text="@string/hello_world"/>
也很简单,冒号里都是注释。整个布局里就放了个TextView控件。

android:id="@+id/text"在资源R的id中给这个控件加个id叫text,以后在代码中可以通过这个id来获取这个控件。

android:layout_width="match_parent"设置控件的宽为填充父控件,这里就是整个屏幕的宽,match_parent等同于fill_parent,只是在android 2.2,即API8中改了名而已。

android:gravity="center_vertical|center_horizontal"设置控件内容垂直居中和水平居中,约束的是view中的内容,要区分android:layout_gravity,这是约束view这个控件整体在布局layout中的位置。

android:text="@string/hello_world"设置控件上的内容为values/strings.xml中hello_world的值<b>Hello,<i>World!</i></b>,<b/>加粗,<i/>倾斜.


这里没难点,layout相关属性,我们以后遇到layout再说。








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值