简体   繁体   中英

How to change default Toolbar title text from @string/app_name to “”, but only changing styles.xml?

I have an app with

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

In some activities I add the ToolBar in xml manually that contains @string/app_name title text by default. I know that I can call setDisplayShowTitleEnabled(false) or something like that, but it does not suit me. I need to other way like changing something in AppTheme so the title text will be changed through all app by default. How to do that?

Toolbar that I added:

<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" />

You can try creating a new layout file and adding the Toolbar code in that. See below this is the layout_app_bar.xml Where you can specify the title of toolbar.

<?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" />

Then in you activities you can include the layout file just like any other tag. you can use this single layout file of toolbar in all activities.

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

I Hope it helped you. Happy Coding!!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM