初学者

来源:互联网 发布:安徽省大数据会员单位 编辑:程序博客网 时间:2024/04/28 17:55

Code:
  1. package org.kevin.android;   
  2.   
  3. import android.app.Activity;   
  4. import android.os.Bundle;   
  5. //HelloAndroid.java   
  6. public class HelloAndroid extends Activity {   
  7.     /** Called when the activity is first created. */  
  8.     @Override  
  9.     public void onCreate(Bundle savedInstanceState) {   
  10.         super.onCreate(savedInstanceState);   
  11.         setContentView(R.layout.main);   
  12.     }   
  13. }  

这个类继承自activity,重载了它的方法onCreate(),其中方法中有一个参数它是activity之前传递数据用的,不知道是不是这样,反正是我理解的,理解万岁!setContentView()方法是布局整个View的此处R.layout.main找到了main.xml,将其显示在屏幕上。Android通过R类找到一些资源,下面是R类的代码

Code:
  1. /* AUTO-GENERATED FILE.  DO NOT MODIFY.  
  2.  *  
  3.  * This class was automatically generated by the  
  4.  * aapt tool from the resource data it found.  It  
  5.  * should not be modified by hand.  
  6.  */  
  7.   
  8. package org.kevin.android;   
  9.   
  10. public final class R {   
  11.     public static final class attr {   
  12.     }   
  13.     public static final class drawable {   
  14.         public static final int icon=0x7f020000;   
  15.     }   
  16.     public static final class layout {   
  17.         public static final int main=0x7f030000;   
  18.     }   
  19.     public static final class string {   
  20.         public static final int app_name=0x7f040001;   
  21.         public static final int hello=0x7f040000;   
  22.     }   
  23. }   

可见Android的activity是通过R找到rec文件夹下的资源的,

那如果想找到app_name的话,只需写R.string.app_name,貌似很简单,

下面在看一下main.xml的内容

Code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView     
  8.     android:layout_width="fill_parent"    
  9.     android:layout_height="wrap_content"    
  10.     android:text="@string/hello"  
  11.     />  
  12. </LinearLayout>  

linearlayout是否是线性布局呢?理解万岁!里面只有一个textview宽为覆盖整个父框架,高为根据内容,内容为string下的hello ID的值。哦出来string了,那不得不看看string.xml

Code:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello World, HelloAndroid!</string>  
  4.     <string name="app_name">HelloAndroid</string>  
  5. </resources>  

果然找到了一个名叫hello的string 其值为Hello World,HelloAndroid!

经过分析,我们貌似得出了一个结论,执行次工程,屏幕上必然显示Hello World,HelloAndroid!

提前忘截图了- -!