You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中如何为各Activity修改Toolbar名称并居中显示

嘿,这两个Toolbar的问题我熟得很,给你一步步讲清楚!

一、修改每个Activity的Toolbar名称

这里有几种常用的方法,你可以根据需求选:

  • 通过AndroidManifest.xml静态设置
    如果你希望每个Activity的标题固定,直接在对应Activity的标签里添加android:label属性就行,Toolbar会自动读取这个值(前提是你用的是AppCompatActivity的默认ActionBar或者绑定了Toolbar作为ActionBar):

    <activity
        android:name=".MainActivity"
        android:label="@string/main_activity_title">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    

    建议把标题内容定义在res/values/strings.xml里,方便后续统一修改。

  • 在Activity代码里动态设置
    如果需要根据逻辑动态改变标题,在Activity的onCreate方法里操作:

    // 如果你用的是Toolbar控件(需要先在布局里定义)
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitle("动态设置的标题");
    
    // 或者如果你直接用默认的ActionBar
    getSupportActionBar().setTitle("动态标题");
    
二、让Toolbar标题居中显示

默认的Toolbar标题是左对齐的,要居中的话,这几种方法最实用:

  • 方法一:自定义Toolbar布局(最可靠)
    自己写一个包含居中TextView的Toolbar布局,灵活性最高:

    1. 创建res/layout/toolbar_custom.xml:
    <?xml version="1.0" encoding="utf-8"?>
    <Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/custom_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary">
    
        <TextView
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="@android:color/white"
            android:textSize="18sp"
            android:layout_gravity="center"/>
    
    </Toolbar>
    
    1. 在Activity的布局里引入这个Toolbar,或者直接作为顶部栏使用;
    2. 在Activity代码里设置标题并隐藏默认标题:
    Toolbar toolbar = findViewById(R.id.custom_toolbar);
    setSupportActionBar(toolbar);
    // 隐藏默认的标题,避免和自定义TextView重复
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    // 设置自定义标题
    TextView toolbarTitle = findViewById(R.id.toolbar_title);
    toolbarTitle.setText("居中的标题");
    
  • 方法二:用MaterialToolbar一键搞定(推荐)
    如果你的项目用了Material Design组件库,直接用MaterialToolbar并开启titleCentered属性就行,超简单:

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/material_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:titleCentered="true"/>
    

    然后不管是通过Manifest的label还是代码setTitle()设置标题,都会自动居中显示。

  • 方法三:动态调整默认Toolbar的标题位置(不推荐,兼容性一般)
    如果你不想自定义布局,可以通过代码找到Toolbar里的标题TextView,修改它的布局参数让它居中,但这种方法可能因为不同版本的Toolbar内部结构变化失效:

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // 遍历Toolbar的子View找到标题TextView
    TextView titleView = null;
    for (int i = 0; i < toolbar.getChildCount(); i++) {
        View child = toolbar.getChildAt(i);
        if (child instanceof TextView) {
            titleView = (TextView) child;
            break;
        }
    }
    if (titleView != null) {
        Toolbar.LayoutParams params = (Toolbar.LayoutParams) titleView.getLayoutParams();
        params.gravity = Gravity.CENTER;
        titleView.setLayoutParams(params);
    }
    

如果还有细节问题随时问哦!

内容的提问来源于stack exchange,提问作者user9524937

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 12:11:25