- package org.kevin.android;
- import android.app.Activity;
- import android.os.Bundle;
- //HelloAndroid.java
- public class HelloAndroid extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- }
这个类继承自activity,重载了它的方法onCreate(),其中方法中有一个参数它是activity之前传递数据用的,不知道是不是这样,反正是我理解的,理解万岁!setContentView()方法是布局整个View的此处R.layout.main找到了main.xml,将其显示在屏幕上。Android通过R类找到一些资源,下面是R类的代码
- /* AUTO-GENERATED FILE. DO NOT MODIFY.
- *
- * This class was automatically generated by the
- * aapt tool from the resource data it found. It
- * should not be modified by hand.
- */
- package org.kevin.android;
- public final class R {
- public static final class attr {
- }
- public static final class drawable {
- public static final int icon=0x7f020000;
- }
- public static final class layout {
- public static final int main=0x7f030000;
- }
- public static final class string {
- public static final int app_name=0x7f040001;
- public static final int hello=0x7f040000;
- }
- }
可见Android的activity是通过R找到rec文件夹下的资源的,
那如果想找到app_name的话,只需写R.string.app_name,貌似很简单,
下面在看一下main.xml的内容
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/hello"
- />
- </LinearLayout>
linearlayout是否是线性布局呢?理解万岁!里面只有一个textview宽为覆盖整个父框架,高为根据内容,内容为string下的hello ID的值。哦出来string了,那不得不看看string.xml
- <?xml version="1.0" encoding="utf-8"?>
- <resources>
- <string name="hello">Hello World, HelloAndroid!</string>
- <string name="app_name">HelloAndroid</string>
- </resources>
果然找到了一个名叫hello的string 其值为Hello World,HelloAndroid!
经过分析,我们貌似得出了一个结论,执行次工程,屏幕上必然显示Hello World,HelloAndroid!
提前忘截图了- -!