主要活动文件
主要活动代码是一个 Java 文件 MainActivity.java。这是最终转换为 Dalvik 可执行文件并运行您的应用程序的实际应用程序文件。以下是应用程序向导为 Hello World 生成的默认代码!应用程序 -</p>
package com.example.helloworld;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
清单文件
无论您开发作为应用程序一部分的任何组件,都必须在位于应用程序项目目录根目录的 manifest.xml 中声明其所有组件。该文件用作 Android 操作系统和您的应用程序之间的接口,因此如果您未在此文件中声明您的组件,则操作系统不会考虑它。例如,默认清单文件将如下所示 -</p>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tutorialspoint7.myapplication">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
字符串文件
strings.xml 文件位于 res/values 文件夹中,它包含应用程序使用的所有文本。例如,按钮、标签、默认文本和类似类型的字符串的名称将进入此文件。该文件对其文本内容负责。例如,默认字符串文件将如下所示 -</p>
<resources>
<string name="app_name">HelloWorld</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_main">MainActivity</string>
</resources>
布局文件
activity_main.xml 是 res/layout 目录中可用的布局文件,您的应用程序在构建其界面时会引用该文件。您将非常频繁地修改此文件以更改应用程序的布局。为您的“Hello World!” 应用程序,该文件将包含以下与默认布局相关的内容 -</p>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".MainActivity" />
</RelativeLayout>