首页 > 解决方案 > 如何将默认工具栏标题文本从@string/app_name 更改为“”,但只更改styles.xml?

问题描述

我有一个应用程序

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

在某些活动中,我在xml中手动添加ToolBar ,其中默认包含@string/app_name标题文本。我知道我可以调用setDisplayShowTitleEnabled(false)或类似的方法,但它不适合我。我需要其他方式,例如在AppTheme中更改某些内容,以便默认情况下通过所有应用程序更改标题文本。 怎么做?

我添加的工具栏:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?actionBarSize"
    android:background="@color/colorAccent"
    app:navigationIcon="@drawable/ic_default_nav_icon_white" />

标签: androidstylestoolbartitle

解决方案


您可以尝试创建一个新的布局文件并在其中添加工具栏代码。见下文,这是 layout_app_bar.xml 您可以在其中指定工具栏的标题。

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/colorPrimary"
android:elevation="4dp"
app:title="WhateverName"
app:titleTextAppearance="@style/AppbarTitle"
app:titleTextColor="@color/colorWhite" />

然后在您的活动中,您可以像任何其他标签一样包含布局文件。您可以在所有活动中使用工具栏的这个单一布局文件。

<LinearLayout>
<include
    android:id="@+id/appbar"
    layout="@layout/layout_app_bar" />
</LinearLayout>

我希望它对你有所帮助。快乐编码!!!


推荐阅读