In android app Toolbar.setTitle method has no effe

2020-01-24 01:46发布

I'm trying to create simple application using android-support-v7:21 library.

Code snippets:
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

<LinearLayout
    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.support.v7.widget.Toolbar            
        android:id="@+id/toolbar_actionbar"
        android:background="@null"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:fitsSystemWindows="true" />

</LinearLayout>

But instead of "My title" on Toolbar %application name% is shown.
Seems like setTitle method has no effect.
I would like to show "My title".

UPD: Before, styles.xml was:

<style name="AppTheme" parent="Theme.AppCompat">
    <item name="windowActionBar">false</item>
</style>

So, I thought that actionbar is not used. I add NoActionBar to style parent:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <item name="windowActionBar">false</item>
</style>

But the problem is not resolved.

26条回答
smile是对你的礼貌
2楼-- · 2020-01-24 02:23

For anyone who needs to set up the title through the Toolbar some time after setting the SupportActionBar, read this.

The internal implementation of the support library just checks if the Toolbar has a title (not null) at the moment the SupportActionBar is set up. If there is, then this title will be used instead of the window title. You can then set a dummy title while you load the real title.

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
mActionBarToolbar.setTitle("");
setSupportActionBar(mActionBarToolbar);

later...

mActionBarToolbar.setTitle(title);
查看更多
等我变得足够好
3楼-- · 2020-01-24 02:24

Found the solution:

Instead of:

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);            
mActionBarToolbar.setTitle("My title");
setSupportActionBar(mActionBarToolbar);

I used:

mActionBarToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);            
setSupportActionBar(mActionBarToolbar);
getSupportActionBar().setTitle("My title");

And it works.

查看更多
冷血范
4楼-- · 2020-01-24 02:26

To set the title for each Navbar fragment title

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    myView = inflater.inflate(R.layout.second_layout, container, false);
    getActivity().setTitle("title");

    return myView;
}
查看更多
女痞
5楼-- · 2020-01-24 02:26

I made it work by using -

toolbar.post(new Runnable() {
            @Override
            public void run() {
                toolbar.setTitle("My Title");
            }
        });
查看更多
Root(大扎)
6楼-- · 2020-01-24 02:26

The answer is in the documentation (which you can find here):

To use the ActionBar utility methods, call the activity's getSupportActionBar() method. This method returns a reference to an appcompat ActionBar object. Once you have that reference, you can call any of the ActionBar methods to adjust the app bar. For example, to hide the app bar, call ActionBar.hide().

That is the solution you actually found. Just thought of giving a reference to the official documentation (which apparently few tend to read).

查看更多
可以哭但决不认输i
7楼-- · 2020-01-24 02:31

Simply you can change any acitivity name by using

Acitivityname.this.setTitle("Title NAme");
查看更多
登录 后发表回答