简体   繁体   English

Android-如何在清单文件中为自定义工具栏设置标题? 标签属性不起作用

[英]Android - How to set title for custom Toolbar in Manifest file? Label attribute doesn't work

I'm using a custom Toolbar widget in my app, I found that the label attribute in Manifest file doesn't work. 我在应用中使用自定义工具栏小部件,发现清单文件中的label属性不起作用。 Any suggestion except using "setTitle()" method in Activity/Fragment? 除了在Activity / Fragment中使用“ setTitle()”方法外,还有其他建议吗? Thank you. 谢谢。

<activity
            android:name=".CartActivity"
            android:label="@string/cart_activity_label"
            android:parentActivityName=".CatalogActivity" />

I wish the title can be shown by using attributes in Manifest file. 我希望可以通过使用清单文件中的属性来显示标题。

use a textview inside the Toolbar widget or if you are using support library, use setsupportActionbar(toolbar); 在工具栏小部件中使用textview,或者如果您正在使用支持库,请使用setsupportActionbar(toolbar); in your activity. 在你的活动中。

getActionBar().setTitle(R.string.logout);

or better, 或更好,

getSupportActionBar().setTitle(R.string.logout);`

in onCreate method 在onCreate方法中

Ok...I just found out that I forgot to link the View (Toolbar) object to the View id. 好的...我刚刚发现我忘记了将View(工具栏)对象链接到View ID。 Follwing is the complete setup for using a Toolbar as a default ActionBar: Follwing是将工具栏用作默认ActionBar的完整设置:

private Toolbar toolbar;

toolbar = findViewById(R.id.toolbar); //I missed this

setSupportActionBar(toolbar);

  • Toolbar title explain: 工具栏标题说明:

    1. You change the default system style to ".NoActionBar", the default ActionBar disappears. 您将默认系统样式更改为“ .NoActionBar”,默认的ActionBar消失。
    2. You add a custom Toolbar to the Layout, an "ActionBar" without title appears. 您将自定义工具栏添加到布局中,将显示不带标题的“ ActionBar”。
    3. There is no title on the Toolbar even though you've already set the label attribute in Manifest file, because the Toolbar is not recognized as default ActionBar by the system, that's why we need to use "setSupportActionbar()" method to make it work like a normal ActionBar. 即使您已经在清单文件中设置了标签属性,工具栏上也没有标题,因为该工具栏未被系统识别为默认的ActionBar,这就是为什么我们需要使用“ setSupportActionbar()”方法使其起作用的原因就像普通的ActionBar。
    4. Don't forget to link the View of the Toolbar. 不要忘记链接工具栏的视图。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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