android:label说明

本文详细介绍了如何在Android应用中通过修改AndroidManifest.xml文件中的activity标签的android:label属性,来实现在不同界面间切换时左上角标题的自动更换。具体包括String.xml中的字符串资源定义、AndroidManifest.xml的配置、以及布局文件和MainActivity.java中的相关代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 说明

android:label 用于app 在切换activity的时候,自动更换左上角的显示

在这里插入图片描述

2. 代码架构

在这里插入图片描述

3. String.xml 字符串资源文件
<resources>
    <string name="app_name">My Application</string>
    <string name="mainactivity">主界面 lab</string>
    <string name="secondactivity">second 界面 lab</string>
</resources>

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        ***android:label="@string/app_name"***        ## application  默认的界面坐上标显示 和 app 名称显示 
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity"
            android:label="@string/mainactivity">  ## 主界面 上的 坐上标显示 和app 名称显示
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity"
            android:label="@string/secondactivity"/>  ## 切换的界面 左上角显示
    </application>

</manifest>
5 布局文件

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textSize="30dp"
        android:onClick="onclick"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>
6 . MainActivity.java

实现一个简单的界面跳转

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void onclick(View view) {
        Intent intent = new Intent(this,SecondActivity.class);
        startActivity(intent);
    }
}
Androidstudio查看这段在AndroidManifest.xml中的代码是否需要修改或者优化<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android"> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@drawable/tubiao" android:label="@string/app_name" android:roundIcon="@drawable/tubiao" android:supportsRtl="true" android:networkSecurityConfig="@xml/network_security_config" android:theme="@style/Theme.MyExamProject"> <activity android:name=".AddActivityActivity" android:exported="false" /> <activity android:name=".AddFurnitureActivity" android:exported="false" /> <activity android:name=".AddCaregiverActivity" android:exported="false" /> <activity android:name=".AddOldActivity" android:exported="false" /> <activity android:name=".FacilityActivity" android:exported="false" /> <activity android:name=".EventsActivity" android:exported="false" /> <activity android:name=".AddActivity2" android:exported="false" /> <activity android:name=".UpdateIFActivity" android:exported="false" /> <activity android:name=".MyIFActivity" android:exported="false" /> <activity android:name=".MainActivity" android:exported="false" /> <activity android:name=".UpdateGamesActivity" android:exported="false" /> <activity android:name=".RegisterActivity" android:exported="false" /> <activity android:name=".PersonalCenterActivity" android:exported="false" /> <activity android:name=".LoginActivity" android:exported="false" /> <activity android:name=".DeleteGamesActivity" android:exported="false" /> <activity android:name=".BackActivity" android:exported="false" /> <activity android:name=".AddGamesActivity" android:exported="false" /> <activity android:name=".HomeActivity" android:exported="true" /> <activity android:name=".WelcomeActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值