Activity类 MainHelloWorld文件浅析
Activity是Android中的视图部分,负责界面显示。
package android.basic.lesson2.helloworld;import android.app.Activity;
import android.os.Bundle;public class MainHelloWorld extends Activity {、
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {、
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
可以看到MainHelloWorld是Activity的子类,子类要重写onCreate方法。setContentView(R.layout.main)方法是给Activity设置可以显示的视图(View),视图由R类负责寻找。
R文件浅析
我们看到Gen目录下有个R.Java文件,R文件由ADT自动生成,程序员不需要也不要去修改它,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 android.basic.lesson2.helloworld;public final class R {
、
public static final class attr {
、
}
public static final class color {、
public static final int red=0x7f050000;
}
public static final class drawable {、
public static final int icon=0x7f020000;
}
public static final class id {
、
public static final int TextView01=0x7f060000;
}
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;
public static final int tagline=0x7f040002;
}
}
从R文件中可以看到每一个资源都会有一个整数和它相对应。
学习更多关于android 源码的知识,可以查询 http://android.9tech.cn/
------解决方案--------------------
很棒!写的不错啊