在Android应用程序中,Toolbar.setTitle方法无效 - 应用程序名称显示为ti

我正在尝试使用android-support-v7:21库创建简单的应用程序。

代码片段:

MainActivity.java

public class MainActivity extends ActionBarActivity {

Toolbar mActionBarToolbar;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);

mActionBarToolbar.setTitle("My title");

setSupportActionBar(mActionBarToolbar);

}

activity_main.xml中

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:fitsSystemWindows="true"

android:orientation="vertical">

android:id="@+id/toolbar_actionbar"

android:background="@null"

android:layout_width="match_parent"

android:layout_height="?actionBarSize"

android:fitsSystemWindows="true" />

但是,不显示工具栏上的“我的标题”,而是显示应用程序名称%。

好像setTitle方法没有效果。

我想展示“我的头衔”。

UPD:之前,NoActionBar是:

false

所以,我认为没有使用动作栏。我将NoActionBar添加到样式父级:

false

但问题没有解决。

非常感谢任何帮助。

23个解决方案

486 votes

找到解决方案:

代替:

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);

mActionBarToolbar.setTitle("My title");

setSupportActionBar(mActionBarToolbar);

我用了:

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);

setSupportActionBar(mActionBarToolbar);

getSupportActionBar().setTitle("My title");

它有效。

krossovochkin answered 2019-02-26T21:54:36Z

102 votes

对于需要在设置SupportActionBar之后通过工具栏设置标题的任何人,请阅读此内容。

支持库的内部实现只是检查工具栏在设置SupportActionBar时是否具有标题(非空)。 如果有,则使用此标题代替窗口标题。 然后,您可以在加载实际标题时设置虚拟标题。

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);

mActionBarToolbar.setTitle("");

setSupportActionBar(mActionBarToolbar);

后来...

mActionBarToolbar.setTitle(title);

sorianiv answered 2019-02-26T21:55:15Z

67 votes

以上答案完全正确,但对我不起作用。

我用以下方法解决了我的问题。

实际上我的XML是这样的:

xmlns:app="http://schemas.android.com/apk/res-auto"

android:id="@+id/confirm_order_mail_layout"

android:layout_width="match_parent"

android:layout_height="fill_parent">

android:id="@+id/confirm_order_appbar_layout"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

android:id="@+id/confirm_order_list_collapsing_toolbar"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:fitsSystemWindows="true"

app:contentScrim="?attr/colorPrimary"

app:expandedTitleMarginEnd="64dp"

app:expandedTitleMarginStart="48dp"

app:layout_scrollFlags="scroll|enterAlways">

android:id="@+id/confirm_order_toolbar_layout"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

android:background="?attr/colorPrimary"

app:layout_scrollFlags="scroll|enterAlways"

app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

我已经尝试了所有的选项,毕竟我刚刚删除了CollapsingToolbarLayout,因为我不需要在特定的Toolbar中使用所以我的最终XML就像:

xmlns:app="http://schemas.android.com/apk/res-auto"

android:id="@+id/confirm_order_mail_layout"

android:layout_width="match_parent"

android:layout_height="fill_parent">

android:id="@+id/confirm_order_appbar_layout"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

android:id="@+id/confirm_order_toolbar_layout"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

android:background="?attr/colorPrimary"

app:layout_scrollFlags="scroll|enterAlways"

app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

现在你必须使用CollapsingToolbarLayout,如上所述:

mActionBarToolbar = (Toolbar) findViewById(R.id.confirm_order_toolbar_layout);

setSupportActionBar(mActionBarToolbar);

getSupportActionBar().setTitle("My Title");

现在如果你想一起使用CollapsingToolbarLayout和Toolbar那么你必须使用setTitle() of CollapsingToolbarLayout

CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.confirm_order_mail_layout);

collapsingToolbarLayout.setTitle("My Title");

愿它能帮到你。 谢谢。

Pratik Butani answered 2019-02-26T21:56:22Z

30 votes

试试这个,你可以直接用XML定义标题:

android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

android:background="?attr/colorPrimary"

app:title="some title"

app:popupTheme="@style/AppTheme.PopupOverlay">

Axrorxo'ja Yodgorov answered 2019-02-26T21:56:47Z

27 votes

您只需使用即可更改任何活动名称

Acitivityname.this.setTitle("Title NAme");

roshan posakya answered 2019-02-26T21:57:12Z

12 votes

设置每个Navbar片段标题的标题

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)

{

myView = inflater.inflate(R.layout.second_layout, container, false);

getActivity().setTitle("title");

return myView;

}

roshan posakya answered 2019-02-26T21:57:37Z

11 votes

试试这个..这个方法对我有用.. !! 希望它可以帮助某人.. !!

xmlns:app="http://schemas.android.com/apk/res-auto"

android:id="@+id/my_awesome_toolbar"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="?attr/colorPrimary"

android:minHeight="?attr/actionBarSize"

app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" >

android:id="@+id/toolbar_title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:singleLine="true"

android:text="Toolbar Title"

android:textColor="@android:color/white"

android:textSize="18sp"

android:textStyle="bold" />

要在工具栏中显示徽标,请尝试以下代码段。//设置drawable

toolbar.setLogo(ContextCompat.getDrawable(context, R.drawable.logo));

让我知道结果。

Ragu Swaminathan answered 2019-02-26T21:58:15Z

9 votes

getSupportActionBar().setTitle("Your Title");

Hanisha answered 2019-02-26T21:58:33Z

8 votes

请参阅[https://code.google.com/p/android/issues/detail?id=77763。]显然它应该像这样工作。 一旦调用setSupportActionBar()方法调用,ActionBar委托就有责任将调用路由到正确的视图。

redDragonzz answered 2019-02-26T21:59:01Z

5 votes

我试图从片段重命名工具栏

它帮助了我,我希望能帮助别人

Activity activity = this.getActivity();

Toolbar toolbar = (Toolbar) activity.findViewById(R.id.detail_toolbar);

if (toolbar != null) {

activity.setTitle("Title");

}

//toolbar.setTitle("Title"); did not give the same results

截图:

Nikita G. answered 2019-02-26T21:59:39Z

5 votes

它不仅仅是mToolbar.setTitle()

mToolbar.setTitle()中支持工具栏(Appcompat v7)的更多方法仅适用于

mToolbar.setTitle()

并且不使用mToolbar.setTitle()

例子:

getSupportActionBar().setTitle("toolbar title");

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

虽然下一个方法没有mToolbar.setTitle() getSupportActionBar().setTitle()正常工作

mToolbar.setVisibility(View.VISIBLE);

mToolbar.setNavigationOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

//

}

问题只有mToolbar.setTitle()事件,你仍然可以使用getSupportActionBar().setTitle()而不是讨厌getSupportActionBar().setTitle(),例如如果你在onCreate中添加它将工作(因为它将在稍后执行,在onCreate之后)

mHandler.post(new Runnable() {

@Override

public void run() {

mToolbar.setTitle("toolbar title");

}

});

我更喜欢使用这个解决方案[https://stackoverflow.com/a/35430590/4548520]而不是[https://stackoverflow.com/a/26506858/4548520],因为如果你多次更改标题(在不同的功能中)它是 比较长getSupportActionBar().setTitle()使用更舒服mToolbar.setTitle()一个你没有看到关于null异常的恼人通知,如getSupportActionBar().setTitle()

user25 answered 2019-02-26T22:00:55Z

4 votes

只需在适配器中使用它,MainActivity是您的AppCompactActivity的位置。从任何地方调用它。

((MainActivity) context).getSupportActionBar().setTitle(titles.get(position));

Axay Coolmoco answered 2019-02-26T22:01:23Z

4 votes

我通过使用 - 使它工作 -

toolbar.post(new Runnable() {

@Override

public void run() {

toolbar.setTitle("My Title");

}

});

Ankit Aggarwal answered 2019-02-26T22:01:50Z

2 votes

试试这个:

Xml代码

android:layout_width="match_parent"

android:layout_height="120dp"

android:id="@+id/tool_bar"

android:background="@color/tablayout"

android:theme="@style/ToolBarStyle"

app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/app_name"

android:layout_gravity="center"

android:id="@+id/toolbar_title"

android:textSize="18sp"

android:textColor="@color/white"/>

Java代码

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

toolbar = (Toolbar) findViewById(R.id.tool_bar);

toolbar_text = (TextView)findViewById(R.id.toolbar_title);

setSupportActionBar(toolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

getSupportActionBar().setDisplayShowTitleEnabled(false);

toolbar.setLogo(R.drawable.ic_toolbar);

}

Janakipathi Chowdary answered 2019-02-26T22:02:26Z

2 votes

如果您使用mToolbar .setTitle(productFromShoppingList.getProductName());

mCollapsingToolbar.setTitle(productFromShoppingList.getProductName());以及Toolbar那么您需要在两个布局中设置标题

在mToolbar .setTitle(productFromShoppingList.getProductName());

mCollapsingToolbar.setTitle(productFromShoppingList.getProductName());方法中将工具栏设置为操作栏

protected void setUpToolBar() {

if (mToolbar != null) {

((HomeActivity) getActivity()).setSupportActionBar(mToolbar);

mToolbar.setTitleTextColor(Color.WHITE);

mToolbar.setTitle("List Detail");

mToolbar.setNavigationOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

getActivity()

.getSupportFragmentManager().popBackStack();

}

});

((HomeActivity) getActivity()).getSupportActionBar()

.setDisplayHomeAsUpEnabled(true);

}

}

稍后只需使用mToolbar .setTitle(productFromShoppingList.getProductName());

mCollapsingToolbar.setTitle(productFromShoppingList.getProductName());方法更新工具栏的标题

mToolbar .setTitle(productFromShoppingList.getProductName());

mCollapsingToolbar.setTitle(productFromShoppingList.getProductName());

Hitesh Sahu answered 2019-02-26T22:03:13Z

2 votes

如果您的目标是在工具栏中设置静态字符串,最简单的方法是在AndroidManifest.xml中设置活动标签:

android:label="@string/string_id" />

工具栏将获得此字符串,不带任何代码。 (适用于v27库。)

Timores answered 2019-02-26T22:03:51Z

2 votes

更改每个不同活动的标题

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_pizza);

setTitle(getResources().getText(R.string.title));

}

roshan posakya answered 2019-02-26T22:04:18Z

1 votes

我有一种奇怪的行为,可以帮助你。

这是有效的,但它仅对onCreate没有影响:

toolbar.setTitle("title");

尝试在onCreate中使用它:

yourActivityName.this.setTitle("title")

user2167877 answered 2019-02-26T22:04:58Z

1 votes

这可以通过在AndroidManifest.xml文件中设置活动的android:label属性来完成:

android:label="The Title I'd like to display" />

然后将此行添加到onCreate():

getSupportActionBar().setDisplayShowTitleEnabled(true);

A-Sharabiani answered 2019-02-26T22:05:31Z

0 votes

确保添加此选项:

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_TITLE);

McLan answered 2019-02-26T22:06:02Z

0 votes

虽然没有立即与这个特定的设置相关,但我发现从我的XML中删除“CollapsingToolbarLayout”将我的工具栏包装在AppBarLayout中使一切正常。

所以这:

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:id="@+id/toolbar"

android:layout_height="?attr/actionBarSize"

app:layout_scrollFlags="scroll|enterAlways"

app:navigationIcon="@drawable/ic_arrow_back_white_24dp" />

而不是这个:

android:layout_width="match_parent"

android:layout_height="wrap_content">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:id="@+id/collapsingToolbar"

android:minHeight="?attr/actionBarSize"

app:layout_scrollFlags="enterAlways|scroll|snap">

android:id="@+id/toolbar"

android:layout_height="?attr/actionBarSize"

app:navigationIcon="@drawable/ic_arrow_back_white_24dp" />

然后,在调用setSupportActionBar()之前,我在活动的onCreate中设置标题。

Chantell Osejo answered 2019-02-26T22:06:51Z

0 votes

答案在文档中(您可以在这里找到):

要使用ActionBar实用程序方法,请调用活动   getSupportActionBar()方法。 此方法返回对a的引用   appcompat ActionBar对象。 一旦你有了这个参考,你可以打电话   用于调整应用栏的任何ActionBar方法。 例如,到   隐藏应用栏,调用ActionBar.hide()。

这是你实际找到的解决方案。 只是考虑提供官方文档(显然很少有人会阅读)。

Ely answered 2019-02-26T22:07:33Z

-3 votes

发生这种情况是因为您正在使用Toolbar和ActionBar。 现在,当您想要使用工具栏作为操作栏时,您需要做的第一件事是禁用装饰提供的操作栏。

最简单的方法是让您的主题从Theme.AppCompat.NoActionBar延伸。

Paresh Mayani answered 2019-02-26T22:08:08Z

androidsettitle方法_在Android应用程序中,Toolbar.setTitle方法无效 - 应用程序名称显示为ti...相关推荐

  1. java list sublist方法_(转)Java 中 List.subList() 方法的使用陷阱

    原文:http://blog.csdn.net/cleverGump/article/details/51105235 前言 本文原先发表在我的 iteye博客: http://clevergump. ...

  2. python字符串截取方法_如何使用python语言中的字符串方法截取字符串

    在我们使用python语言中的字符串方法时,可能会判断某个字符串是否以什么开头,可以使用什么进行截取等.下面利用几个实例说明字符串中的方法的用法,操作如下: 工具/原料 python 截图工具 方法/ ...

  3. java方法重载和重载方法_我们可以在Java中重载main()方法吗?

    java方法重载和重载方法 The question is that "can we overload main() method in Java?" 问题是"我们可以在 ...

  4. 微信小程序中嵌套html_在微信小程序中渲染HTML内容3种解决方案及分析与问题解决...

    大部分Web应用的富文本内容都是以HTML字符串的形式存储的,通过HTML文档去展示HTML内容自然没有问题.但是,在微信小程序(下文简称为「小程序」)中,应当如何渲染这部分内容呢? 在微信小程序中渲 ...

  5. SQL Server 2005 - 如何在预存程序中调用另外一个预存程序

    要在一个预存程序中调用另外一个预存程序,可以使用下列两种方式之一进行调用:  <?xml:namespace prefix = o /> EXECUTE <欲执行之预存程序的名称&g ...

  6. java settitle_关于java的JFrame中的setTitle()方法

    展开全部 java的JFrame中使用setTitle()方法设置窗体的标题: 下面是一个计算器的例子: import java.awt.BorderLayout; import java.awt.D ...

  7. python代码执行过程记录_详解python程序中记录日志的方法

    日志可以用来记录应用程序的状态.错误和信息消息,也经常作为调试程序的工具.它的重要性就不多说了,直接进入正题. python提供了一个标准的日志接口,就是logging模块.日志级别有DEBUG.IN ...

  8. python中import cv2遇到的错误及安装方法_独家利用OpenCV,Python和Ubidots来构建行人计数器程序(附代码amp;解析)...

    作者:Jose Garcia 翻译:吴振东 校对:张一豪 本文约4000字,建议阅读14分钟. 本文将利用OpenCV,Python和Ubidots来编写一个行人计数器程序,并对代码进行了较为详细的讲 ...

  9. 微信小程序中嵌套html_在微信小程序中渲染HTML内容的方法示例

    大部分Web应用的富文本内容都是以HTML字符串的形式存储的,通过HTML文档去展示HTML内容自然没有问题.但是,在微信小程序(下文简称为「小程序」)中,应当如何渲染这部分内容呢? 解决方案 wxP ...

  10. aop执行跳过某个方法_简谈前端开发中的AOP(一) -- 前端AOP的实现思路

    前言 本意不想用太长的篇幅,来阐述这个话题.但是有些概念和设计思路有必要讲清楚,以便于搞清楚其深层次的内在逻辑.这是我一直遵从的"知其然,知其所以然"的原则.首先,本文将简单的阐述 ...

最新文章

  1. TCP的三次握手和四次分手
  2. 《课程的反思与重建--我们需要什么样的课程观》之心得体会
  3. hive 列转行_掌握这个SQL技巧超越80%的人——行转列/列转行
  4. 【引用】整理Sed与Awk学习笔记(一)
  5. Unity3D游戏-愤怒的小鸟游戏源码和教程(二)
  6. iOS 7 二维码的生成
  7. Oracle自定义函数
  8. python怎么判断是不是汉字危机_谈 Python 的中文编码处理
  9. 如何在Android Wear上节省电池寿命
  10. 离散数学关系的性质_关系和关系的性质| 离散数学
  11. struts2 表单提交乱码问题解决办法
  12. 怎样成为优秀软件模型设计者
  13. Quartus II bilibili 入门 EDA实用技术教程(一)---verilog语言编程基础
  14. 异地监控组网实战案例(速度快)
  15. 程序员如何巧用Excel提高工作效率
  16. HTML常用基础知识归纳
  17. html2canvas生成放大图片
  18. 通达信地量指标公式 启涨地量买点选股指标天眼地量指标
  19. p2020开发_10个使您在2020年获得第一份开发工作的项目
  20. 色深 (Color Depth)

热门文章

  1. 【快代理API】获取隧道代理IP
  2. Netbox 开源 IPAM 管理工具搭建详细流程
  3. btrfs管理及应用
  4. 第三方支付平台:微信支付接口
  5. 刷题集--贪吃的九头龙
  6. IMX6UL GPIO复用
  7. Myeclipes 10 cracker.jar破解文件指定路径错误
  8. 初中OJ1998【2015.8.3普及组模拟赛】饥饿的WZK(hunger)
  9. ArcGIS 教程:Workflow Manager 快速浏览
  10. Solr评分整理汇总:深入理解Lucene默认打分算法以及常用的三种评分方法