【问题标题】:How to change the toolbar name and put icon with tool bar如何更改工具栏名称并使用工具栏放置图标
【发布时间】:2026-02-22 07:20:03
【问题描述】:

我刚刚使用导航视图的 android studio 模板制作了应用程序。一切正常,但我有以下两个要求

  1. 我想使工具栏标题居中。
  2. 我想把图片放在标题旁边。

这是我的 xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:fitsSystemWindows="true"
tools:context="com.abdulsalam.lahoreqalander.MainActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main" />

<!--<android.support.design.widget.FloatingActionButton-->
    <!--android:id="@+id/fab"-->
    <!--android:layout_width="wrap_content"-->
    <!--android:layout_height="wrap_content"-->
    <!--android:layout_gravity="bottom|end"-->
    <!--android:layout_margin="@dimen/fab_margin"-->
    <!--android:src="@android:drawable/ic_dialog_email" />-->

问题:

我不知道标题字符串保存在哪里,我检查了 string.xml 并且有应用程序名称,并且它完全显示在工具栏标题上。

所以任何建议我可以在哪里更改标题字符串而不更改应用程序名称,事实上我想自定义更改它以及如何将图像放在它旁边。

注意我使用的是Android studio制作的模板

【问题讨论】:

  • getSupportActionBar().setTitle("Material Design Samples");
  • getSupportToolBar() 将帮助您获取标题和可绘制对象
  • 但是如何设置重力
  • 您必须为工具栏集创建自己的样式而不是颜色*.com/a/29533038/3514144
  • 而且在 xml 中以设计方式处理它不是更好吗,我想知道在 java 中的代码和 xml 中没有为标题设置字符串,但它是如何使用的该字符串的标题

标签: android android-studio android-toolbar


【解决方案1】:

您可以在 XML 中这样实现。

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay">

    <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:orientation="horizontal"
     android:layout_gravity="center">

          <ImageView
            android:src="@mipmap/ic_launcher"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

          <TextView
            style="@style/TextAppearance.Widget.AppCompat.Toolbar.Title"
            android:text="@string/app_name"
            android:background="?attr/selectableItemBackground"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

</android.support.v7.widget.Toolbar>

【讨论】:

    【解决方案2】:

    在你的活动课上做这个创建

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    toolbar.setTitle("example");
    toolbar.setLogo(R.drawable.icon);
    

    或编辑字符串资源并更改标题值和图标

    【讨论】:

    • 编辑字符串资源,可以看到设置了哪个字符串资源,如代码所示
    • 在java中也没有办法改变重力
    • 签入清单文件,活动将伴随标签
    • getSupportToolbar 找不到
    【解决方案3】:

    首先来自the documentation

    不鼓励在 API 21 及更高版本的设备上使用应用程序图标加标题作为标准布局。

    要通过 xml 设置图标,您可以在工具栏中使用 android:navigationIcon。 对于设置文本,我也会使用 Java 代码方法,因为我在文档中找不到任何带有 xml 的内容。无论如何,文本不会居中。

    如果要使文本居中,则必须修改工具栏。 Toolbar 是一个视图,您可以在 Toolbar 中放置任何您想要的内容。

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingRight="@dimen/activity_horizontal_margin"
            android:paddingEnd="@dimen/activity_horizontal_margin"         
            android:id="@+id/toolbar_content">
    
            // define text and icon here
    
        </RelativeLayout>
    
    </android.support.v7.widget.Toolbar>
    

    【讨论】: