簡體   English   中英

如何將默認工具欄標題文本從@string/app_name 更改為“”,但僅更改 styles.xml?

[英]How to change default Toolbar title text from @string/app_name to “”, but only changing 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>

我希望它對你有所幫助。 快樂編碼!!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM