Activity
项目中的任何活动都应该重写Activity的onCreate()方法,而目前我们的FirstActivity中已经重写了这个方法,这是由Android Studio自动帮我们完成的。代码如下:
public class FisrtActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //调用父类的onCreate(),当然这只是默认的实现,后面我们还需要在里面加入很多自己的逻辑。 } }
Android程序的设计讲究逻辑和视图分离,最好每一个活动都能对应一个布局。
新建layout目录并新建first_layout:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> </LinearLayout>
由于我们刚才在创建布局文件时选择了LinearLayout作为根元素,因此现在布局文件中已经有一个LinearLayout元素了.
新加一个Button
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <!--android:id是给当前的元素定义一个唯一标识符,之后可以在代码中对这个元素进行操作--> <!--如果需要在XML中引用一个id,就使用@id/id_name这种语法,而如果需要在XML中定义一个id,则要使用@+id/id_name这种语法--> <!--android:layout_width指定了当前元素的宽度,match_parent表示让当前元素和父元素一样宽--> <!--android:layout_height指定了当前元素的高度,wrap_content表示当前元素的高度只要能刚好包含里面的内容就行--> <!--android:text指定了元素中显示的文字内容--> <Button android:id="@+id/button_1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button 1" /> </LinearLayout>
接下来我们要做的,就是在活动中加载这个布局:可以看到,这里调用了setContentView()方法来给当前的活动加载一个布局,而在setContentView()方法中,我们一般都会传入一个布局文件的id。在第1章介绍项目资源的时候我曾提到过,项目中添加的任何资源都会在R文件中生成一个相应的资源id,因此我们刚才创建的first_layout.xml布局的id现在应该是已经添加到R文件中了。在代码中去引用布局文件的方法你也已经学过了,只需要调用R.layout.first_layout就可以得到first_layout. xml布局的id,然后将这个值传入setContentView()方法即可。
package com.example.activitytest; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class FisrtActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.first_layout); } }
在AndroidManifest文件中注册别忘了在上一章我们学过,所有的活动都要在AndroidManifest.xml中进行注册才能生效,而实际上FirstActivity已经在AndroidManifest.xml中注册过了,打开app/src/main/Android-Manifest.xml文件瞧一瞧,活动的注册声明要放在<application>标签内,通过<activity>标签来对活动进行注册,andriod studio帮我们自动完成了对FirstActivity的注册.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.activitytest"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Activitytest"> <activity android:name=".FisrtActivity"></activity> </application> </manifest>
.FirstActivity是什么意思呢?其实这不过就是com.example.activitytest.FirstActivity的缩写而已,由于在最外层的<manifest>标签中已经通过package属性指定了程序的包名是com.example.activitytest,因此在注册活动时这一部分就可以省略了,直接使用.FirstAc-tivity就足够了。
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.activitytest"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/Theme.Activitytest"> <activity android:name=".FisrtActivity" android:label="This is FisrtActivity" > <intent-filter > <action android:name = "android.intent.action.MAIN"> </action> <category android:name = "android.intent.category.LAUNCHER"> </category> </intent-filter> </activity> </application> </manifest>
仅仅注册了活动,我们的程序仍然是不能运行的,因为还没有为程序配置主活动,也就是说,当程序运行起来的时候,不知道要首先启动哪个活动。配置主活动的方法其实在第1章中已经介绍过了,就是在<activity>标签的内部加入<intent-filter>标签,并在这个标签里添加<actionandroid:name="android.intent.action.MAIN"/>和<categoryandroid:name="android.intent.category.LAUNCHER" />这两句声明即可。除此之外,我们还可以使用android:label指定活动中标题栏的内容,标题栏是显示在活动最顶部的,待会儿运行的时候你就会看到。需要注意的是,给主活动指定的label不仅会成为标题栏中的内容,还会成为启动器(Launcher)中应用程序显示的名称。