(android第一行代码)手动创建一个活动。

    一 活动是什么:

                活动(Activity)是最容易吸引到用户的地方了,它是一种可以包含用户界面的组件, 主要用于和用户进行交互。一个应用程序中可以包含零个或多个活动

     二 活动的基本用法:

                1.创建活动:

  •                                     新建一个 Android项目,,项目名可以叫做 ActivityTest。
  •                                     包名我们就使用 默认值 com.example.activitytest。
  •                                     我们不再勾选 Create Activity这个选项,因为这次我们准备手动创 建活动。

                      2. 创建Activity:

  •                             在ActivityTest项目的 src目录下,添加一个包默认包名 com.example.activitytest,点击 Finish。
  •                             们新建 一个名为 FirstActivity的类,并让它继承自 Activity,点击 Finish完成创建。
  •                             在FirstActivity中重写onCreate() 方法:

                                     public class FirstActivity extends Activity {

                                         @Override

                                      protected void onCreate(Bundle savedInstanceState) { 

                                            super.onCreate(savedInstanceState); 

                                           }  

                                    }

                          3.创建和加载布局:

  •                         创建 first_layout.xml
  •                                对这个布局稍做编辑,添加一个按钮,

                         <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

                             android:layout_width="match_parent"  

                             android:layout_height="match_parent" 

                             android:orientation="vertical" >   

                          <Button   

                                 android:id="@+id/button_1"    

                                 android:layout_width="match_parent"   

                                 android:layout_height="wrap_content"  

                                 android:text="Button 1"   

                                 /> 

                     </LinearLayout>

               4.          

                      重新回到 FirstActivity,在 onCreate()方法中加入如下代码:

                              public class FirstActivity extends Activity {  

                                      @Override 

                               /* 这里调用了 setContentView()方法来给当前的活动加载一个布局

                                  调用 R.layout.first_layout 就可以得到 first_layout.xml 布局的 id,

                                    然后将这个值传入 setContentView()方法即可*/

 

                                 protected void onCreate(Bundle savedInstanceState) {  

                                       super.onCreate(savedInstanceState);  

                                       setContentView(R.layout.first_layout);

                               } 

                    }

                5.   在 AndroidManifest 文件中注册 :

                       所有的活动都要在 AndroidManifest.xml中进行注册才能生效.

                  AndroidManifest.xml文件

                         <manifest xmlns:android="http://schemas.android.com/apk/res/android"

                                    package="com.example.activitytest"  

                                    android:versionCode="1"  

                                    android:versionName="1.0" >   

                                   <uses-sdk    

                                       android:minSdkVersion="14"       

                                       android:targetSdkVersion="19" /> 

                             //    注册声明要放在<application>标签内

                                   <application                                         

                                        android:allowBackup="true"     

                                        android:icon="@drawable/ic_launcher"  

                                        android:label="@string/app_name"   

                                        android:theme="@style/AppTheme" >  

                                                <activity         

                                       //android:name来指定具体注册哪一个活动

                                                       android:name=".FirstActivity"     

                                        //android:label指定活动中标题栏的内容,标题栏是显示在活动最 顶部的  运行的时候你就会看到。 

                                                        android:label="This is FirstActivit/>

                                       //  让 FirstActivity 作为我们这个程序的  主活动.

                                                          <intent-filter>        

                                                                  <action android:name="android.intent.action.MAIN" />  

                                                                  <category android:name="android.intent.category.LAUNCHER" /> 

                                                         </intent-filter>     

                                                  </activity>  

                                         </application>

                               </manifest>

           6. 隐藏标题栏
                           protected void onCreate(Bundle savedInstanceState) { 

                                             super.onCreate(savedInstanceState);

                                             requestWindowFeature(Window.FEATURE_NO_TITLE); //表示无标题。

                                             setContentView(R.layout.first_layout);

                         }

 

 

                                                    

                                            

     

 

             

posted on 2016-11-24 20:23  blogchen  阅读(818)  评论(0编辑  收藏  举报

导航