移动应用开发之路 02 Android Studio 项目文件介绍(Activity, 布局文件, 清单文件)详解

环境

用的环境是Android Studio,Windows 桌面下的图标长这样(plz忽略水印):

在这里插入图片描述

我的配置是:

Android Studio Chipmunk | 2021.2.1 Patch 2
Build #AI-212.5712.43.2112.8815526, built on July 10, 2022
Runtime version: 11.0.12+7-b1504.28-7817840 amd64
VM: OpenJDK 64-Bit Server VM by Oracle Corporation
Windows 10 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 1280M
Cores: 8
Registry: external.system.auto.import.disabled=true
Non-Bundled Plugins: Dart (212.5744), com.mobiledi.flutter_plugins (2.0.0), io.flutter (71.0.2)

在这里插入图片描述

Activity

Activity大概包括3个比较重要的点

  • MianActivity
  • onCreate()
  • setContentView()

关于Activity类,举个例子:

public class lalalala	// 这个就是一个普通的类,没有任何作用。
public class lalalala extends Activity		// 这个就是一个可以投到手机上的可视化的类

我们拿出一段平平无奇的activity,简要介绍一下它的机制(具体的介绍都在注释里了):

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    // Activity: 是一个类,AppCompatActivity也是算是Activity类。类似于一个可视化界面
    @Override   // 表示这个是重写,只要打开窗口,指定先执行它,所以有点类似于初始化代码
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置内容视图
        // R会按照类别给资源文件分配一个索引 => 我们可以通过R.类别名.资源名 去操作对应的资源
        setContentView(R.layout.activity_main);
    }
}

布局文件

我们知道activity里面setContentView是根据布局文件layout完成的,所以这一整个项目究竟显示的东西都由它决定。这个不同于java的写法,主要是xml代码。就是通过一个个标签和控键设置布局。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="#999">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="40dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

所以下次我们如果想要创建新的文件,就应该在layout下生成:

在这里插入图片描述

清单文件

这个是决定各个窗口的逻辑的文件,比如显示哪个窗口之类的。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.MyApplication"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
<!--                 启动界面-->
                <action android:name="android.intent.action.MAIN" />
<!--                 在应用列表形成图标-->
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>
  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

铖铖的花嫁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值