《第一行代码》第二章笔记-设置主活动

1.布局中添加按钮:

 在   first_layout.xml里添加按钮        

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


    <Button
        android:id="@+id/button_1"              //设置id为button_1
        android:layout_width="match_parent"    //宽度匹配界面
        android:layout_height="wrap_content"   //高度取合适,包含即可
        android:text="Button_1" />             //按钮text

    
</LinearLayout>


2.加载布局:

FirstActivity中加载布局

 
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.first_layout);
}

项目添加的任何资源都会在R文件中生成一个相应的资源id.

故调用R.layout.first_layout就可以得到first_layout.xml的id,然后传入setContentView.


3.在AndroidManifest中注册:

注意! 是Manifest不是Mainfest!

AndroidStudio其实已经帮我们注册了.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.zx.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/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>

我们需要配置主活动,即应用开启时启动哪个活动.

在activity标签内部加上Intent-filter标签,并码上图中代码.


这样我们就设置FirstActivity为主程序啦.

这是最基本的步骤,活动-布局-注册-设置活动为主活动.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值