Android的第一个程序

摘要:对于安卓的历史和安卓需要学习哪些东西以及怎么安卓环境,我就不在这里多说了,网上一大推。

我这里主要说的就是代码。一些基础的安卓知识。在接下来的每个月里我都会不定期写一些博客给初学者学习,我会尽量附上相关的视图。

(里面也可能包括一些我在项目中遇到的东西,来丰富大家的知识。)  备注:也请大家多多指教。

下面我们先说android的第一个程序。

一:主要有三样东西(Activity,xml文件,AndroidManifest.xml文件)。

1.Activity

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);//此处配置xml文件
    }
  //下面的两个方法可以删掉,这里我们不需要。
    @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;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

2.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"
    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="com.android.page.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="你好,我是第一个页面!" />

</RelativeLayout>

3.AndroidManifest.xml文件(主要配置Activity)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.page"
    android:versionCode="1"
    android:versionName="1.0" >

    <!--
    minSdkVersion 表示sdk最小的版本
    targetSdkVersion表示目标版本,要比最小的大,不能超过当前的版本
    -->
    <uses-sdk
        android:minSdkVersion="19"
        android:targetSdkVersion="19" />
    <!--
         allowBackup   它表示是否允许应用程序参与备份。
        icon 表示应用的图标 
         android:label:应用的标签 
         android:theme:应用的主题
    -->
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>

                <!--
                      一个应用程序可以有多个Activity,每个Activity是同级别的,那么在启动程序时,
                 最先启动哪个Activity呢?有些程序可能需要显示在程序列表里,有些不需要。怎么定义呢?
     android.intent.action.MAIN决定应用程序最先启动的Activity android.intent.category.LAUNCHER决定应用程序是否显示在程序列表里
            因为你的程序可能有很多个activity 只要xml配置文件中有这么一个intent-filter,而且里面有这个launcher,
             那么这个activity就是点击程序时最先运行的那个activity。
                -->
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
原文地址:https://www.cnblogs.com/wuziyue/p/5371717.html