Remove title in Toolbar in appcompat-v7

2019-01-21 00:57发布

问题:

The documentation of Toolbar says

If an app uses a logo image it should strongly consider omitting a title and subtitle.

What is the proper way to remove the title?

回答1:

getSupportActionBar().setDisplayShowTitleEnabled(false);


回答2:

The correct way to hide/change the Toolbar Title is this:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(null);

This because when you call setSupportActionBar(toolbar);, then the getSupportActionBar() will be responsible of handling everything to the Action Bar, not the toolbar object.

See here



回答3:

Try this...

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

    .....

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_landing_page);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    .....

 }


回答4:

The reason for my answer on this is because the most upvoted answer itself failed to solve my problem. I have figured out this problem by doing this.

<activity android:name="NAME OF YOUR ACTIVITY"
    android:label="" />

Hope this will help others too.



回答5:

Another way to remove the title from your Toolbar is to null it out like so:

Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
toolbar.setTitle(null);


回答6:

If you are using Toolbar try below code:

toolbar.setTitle("");


回答7:

You can use any one of bellow as both works same way: getSupportActionBar().setDisplayShowTitleEnabled(false); and getSupportActionBar().setTitle(null);

Where to use:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

Or :

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setTitle(null);


回答8:

 Toolbar actionBar = (Toolbar)findViewById(R.id.toolbar);
    actionBar.addView(view);
    setSupportActionBar(actionBar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

take note of this line getSupportActionBar().setDisplayShowTitleEnabled(false);



回答9:

I dont know whether this is the correct way or not but I have changed my style like this.

<style name="NoActionBarStyle" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
</style>


回答10:

this

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    //toolbar.setNavigationIcon(R.drawable.ic_toolbar);
    toolbar.setTitle("");
    toolbar.setSubtitle("");
    //toolbar.setLogo(R.drawable.ic_toolbar);


回答11:

The correct way to hide title/label of ToolBar the following codes:

  Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setTitle(null);


回答12:

Nobody mentioned:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
    }


回答13:

toolbar.setTitle(null);// remove the title



回答14:

Just add this attribute to your toolbar

app:title=" "


回答15:

Toolbar toolbar = findViewById(R.id.myToolbar);
    toolbar.setTitle("");