Android开发:Hello World!

来源:互联网 发布:淘宝开店认证2张银行卡 编辑:程序博客网 时间:2024/04/28 02:56

Hello World

Hello World已是各种编程入门的第一个经典例子,这里也不例外。

创建一个新的工程

这里写图片描述

目录的含义

CreateMyActivity是工程名称,有很多的文件夹和文件,需要了解的有app这个文件夹
展开:
这里写图片描述
其中lib文件夹存放第三方JAR包,src放置代码。
进一步展开:
这里写图片描述
main内包含java和res,java内有程序代码(和JAVA开发一样),res内含有资源文件,layout内放置布局,mip-map内放置图片,hdpi,ldpi,mdpi代表不同的分辨率(暂时不需要关注),value内有键值。重要是的AndroidManifest.xml这个文件,粗略来讲,Android由一个个活动Activity构成,所有的活动都要在这个文件内注册。

Hello World!

在2.1.3版本中所有的活动都继承自基类AppCompatActivity,重写oncreate:

public class MyActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.mylayout);    }}

其中mylayout是自己的布局,存放在res的layout中,R.layout.mylayout为参数(mylayout的路径)传入setContentView,setContentView用了显示布局。而super.onCreate(savedInstanceState)就是创建该活动的实例。这样一个活动就写好了,但是还没有被注册到AndroidManifest.xml文件中。

在AndroidManifest.xml中注册:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.administrator.createmyactivity">    <application        android:allowBackup="true"        //启动图标:在layoutmipmap.xml定义的ic_lanucher        android:icon="@mipmap/ic_launcher"        //应用名称:在valuestring.xml定义的app_name        android:label="@string/app_name"        android:supportsRtl="true"        //应用:在layoutstyle.xml定义的AppTheme        android:theme="@style/AppTheme">        //.MyActivity 是定义的活动的相对路径 package已经指明在哪个包下        <activity android:name=".MyActivity"            android:label="OK">            //意图“intent”配置,此处是为了使MyActivity作为主活动            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>

layout的介绍:
mylayout.xml如下

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    //布局宽度和高度,match_parent即匹配自父布局    android:layout_width="match_parent"    android:layout_height="match_parent">    //新建一个按钮<TextView    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="@string/hello_world"/></LinearLayout>

运行即是Hello World!

0 0
原创粉丝点击