本文介绍了getSupportActionBar().setTitle()与工具栏.setTitle()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有两种方法可以在Android Activity 中设置标题.

I am aware that there are two methods to setting a title in an Android Activity.

假设我已经有以下代码...

Assuming I already have the following code...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_activity);

    ...

    Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
    setSupportActionBar(toolbar);

...我可以用这个...

...I can use either this...

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

...或者这个...

...or this...

toolbar.setTitle("My title");

...设置我的标题.

...to set my title.

我的问题是,哪种方法更好?

推荐答案

如果您呼叫setSupportActionBar(Toolbar),然后操作栏负责处理标题,因此您需要调用getSupportActionBar().setTitle("My Title");设置自定义标题.

If you call setSupportActionBar(Toolbar),then the Action Bar is then responsible for handling the title, therefore you need to call getSupportActionBar().setTitle("My Title"); to set a custom title.

也请检查此链接,其中toolbar.setTitle("My title");可能会引起如下问题:-在Android应用程序Toolbar.setTitle中方法无效-应用程序名称显示为标题

Also check this link where toolbar.setTitle("My title"); may cause problem like below:-In android app Toolbar.setTitle method has no effect – application name is shown as title

工具栏是操作栏的一般形式.

And toolbar is the general form of action bar.

我们可以有多个工具栏作为布局小部件,但操作不是.

We can have multiple toolbars as layout widget but action is not.

因此,更好的方法是使用getSupportActionBar().setTitle("My Title");

Thus better approach is to use getSupportActionBar().setTitle("My Title");

这篇关于getSupportActionBar().setTitle()与工具栏.setTitle()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 04:20