android - 如何将默认工具栏标题文本从@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" />
解决方案
您可以尝试创建一个新的布局文件并在其中添加工具栏代码。见下文,这是 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>
我希望它对你有所帮助。快乐编码!!!
推荐阅读
- javascript - ReferenceError:导航器未在 angular-universal-pwa 中定义
- python-3.x - 努力寻找不同氨基酸的数量
- r - 根据其他列的其他行中的值创建一个新列
- vue.js - 数据表VueJS不显示数据
- django - Django:ModelForm在clean函数中获取ManyToMany字段
- build - Lineageos 培根构建失败
- c#-5.0 - 如何在 .net 核心 WebApi 中为用户注册用例构建 .NET 无状态状态机
- angular - 当您无法设置回调函数名称时,如何使用 Angular 向静态文件发出 JSONP 请求?
- python - 加权肯德尔 Tau
- sql - MS Access - 带有 2 个文本框的主菜单表单,用于打开和过滤不同的表单 - 类型不匹配(运行错误 13)