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

在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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值