第一个Android程序

来源:互联网 发布:skt皮肤盲僧淘宝价多少 编辑:程序博客网 时间:2024/06/04 20:56
开发工具选择Eclipse,具体的下载以及配置过程http://blog.csdn.net/lpftobetheone/article/details/8142584
当我们新建一个Android工程文件后的目录如下
1.src文件夹:是源代码文件夹
2.res中包含的比较多:只要和视图有关系的资源文件都在这里面,包括图片资源放在几个drawable文件夹中,布局文件放在layout中,values中存放的是style.xml、strings.xml和colors.xml。
    2.1strings.xml中具体的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="app_name">App框架</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">设置</string>    
    <!-- Login -->
    <string name="account">账号</string>
    <string name="pwd">密码</string>
    <string name="login_in">登录</string>
    <string name="cancel">取消</string>
    <string name="forget_pwd">忘记密码?</string>
</resources> 
 其实这里面的内容只是一些界面上显示的字符,这是为了国际化而设计的,个人感觉为了便于修改。
    2.2drawable里面放的是图片资源,为了适应不同的分辨率所以同一张图片要做多个不同的分辨率。
3.AndroidManifest.xml这是整个Android程序的清单布置文件,这个文件比较重要。里面的内容如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.king.appwork"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="8" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.king.appwork.LoginActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest> 
里面的内容共的含义,以后会介绍到,现在只是了解一下。 
4.gen文件夹,是系统自动生成的文件夹里面最重要的就是R.java文件,向res中添加一个文件后,会自动在R.java中生成一个记录,由于是系统自动生成的所以我们现在不必要关注。

了解完目录结构后我们就开始写第一行Android程序了,在Eclipse中点击New->AndroidProject->填写工程名字然后一路Next知道Finish,就可以生成一个空的Android工程。目录如下:

上面我们说到在res文件夹下的layout里面都一些布局文件,这些布局文件都是Xml格式的,它们决定了我们Android程序的布局样式,因此我们要先要为我们的程序布局显示样式。具体代码如下:
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</RelativeLayout>

MainActivity.java的具体代码如下:
package com.example.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}
然后在项目上右键->run as->Android Application,就可以启动我们的第一个程序了,虽然我们什么都没做,但是我们还是得到了一个空的Android工程结果如下:


0 0