第一行代码阅读笔记---详解分析第一个Android程序

来源:互联网 发布:linux 多进程 信号 编辑:程序博客网 时间:2024/04/29 00:10

以下是我根据作者的思路,创建的第一个Android应用程序,由于工具强大,代码都自动生成了,如下:

package com.example.first_app;import android.os.Bundle;import android.app.Activity;import android.view.Menu;//MainActivity继承于Activity类  此处用到了java关键字extendspublic class MainActivity extends Activity {    @Override    //该方法是活动被创建的时候必须要执行的方法。    protected void onCreate(Bundle savedInstanceState) {    //使用super关键字,达到子类调用父类的效果,这里MainActivity是子类,    //而Activity是父类,它们是继承关系。        super.onCreate(savedInstanceState);        //这个方法就是个当前的Activity引入的一个layout布局        //布局文件位于res目录的layout目录下        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;    }    }
那上面所说的布局文件的代码如下:位于res目录下的layout下的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=".MainActivity" >//这个TextView是Android系统提供的一个控件,用于布局中显示文字的控件。    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/hello_world" />
那么,我们所看到hello world!字符串究竟被定义在哪里呢?

在res目录下values目录下的strings.xml,这个文件专门用来保存字符串。

<?xml version="1.0" encoding="utf-8"?><resources>//这个app_name可以对其进行修改,这样可以用来改变应用程序的名称    <string name="app_name">first_app</string>    <string name="action_settings">Settings</string>//下面这个hello world!就是被定义在这里    <string name="hello_world">Hello world!</string>   </resources>



0 0
原创粉丝点击