Activity活动介绍

1. 什么是活动
    它是一种可以包含用户界面的组件,主要用于和用户进行交互。
2. 活动的基本用法
    新建活动:
        src下新建一个class: FirstActivity 继承 Activity 如下:

public class FirstActivity extends Activity {

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        

         setContentView(R.layout.first_layout);

    }   

}

项目中的任何活动都应该重写Activity的onCreate()方法。

创建和加载布局文件

Android程序的设计讲究逻辑和视图分离,最好每一个活动都能对应一个布局,布局就是用来显示界面内容的;

新建布局文件:

res/layout目录下新建Android XML File 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>
android:id 是给当前元素定义一个唯一标识符
@+id/id_name:在XML元素中定义一个id
@id/id_name: 在XML中引用一个id
android:layout_width指定当前元素的宽度,这里使用match_parent表示当前元素和父元素一样宽。
android:layout_height指定了当前元素的高度,这里使用wrap_content表示当前元素的高度只要能刚好包含里面的内容就行。
android:text 指定元素中显示的文字内容。
加载布局文件

setContentView(R.layout.first_layout);

setContentView()方法来给当前的活动加载一个布局,一般需要传入一个布局文件的id。前面提到过,只需要调用R.layout.first_layout就可以得到first_layout.xml布局的id,然后将它传入setContentView()即可。

注意:这里的R指的是项目包下的R文件。Android SDK还会自动提供一个android包下的R文件。

在AndroidManifest文件中注册

所有的活动必须在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

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name=".FirstActivity"

            android:label="This is FirstActivity" >

            <intent-filter>

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

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

            </intent-filter>

        </activity>

    </application>

</manifest>

活动的声明必须放在<application>标签中,通过<activity>标签来对活动进行注册。

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

.FirstActivity:表示com.example.activitytest.FirstActivity的缩写; 由于最外层的<manifest>标签中已经通过package属性指定了程序的包名,因此注册活动时这一部分可以省略。

android:label 指定活动中标题栏的内容,标题栏是显示在活动最顶部的。给活动指定的label不仅会成为标题栏中的内容,还会成为启动器中应用程序显示的名字。

<intent-filter>主活动声明

如果你的应用程序中没有声明任何一个活动作为主活动,这个程序仍然是可以正常安装的,只是你无法在启动器中看到或者打开这个程序。这种程序一般都是作为第三方服务供其他的应用在内部进行调用的,如支付宝快捷支付服务。
至此已经可以创建并运行自己的活动了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值