Remove title in Toolbar in appcompat-v7

前端 未结 18 1410
孤街浪徒
孤街浪徒 2020-12-07 09:52

The documentation of Toolbar says

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

相关标签:
18条回答
  • 2020-12-07 10:26
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    
    0 讨论(0)
  • 2020-12-07 10:30

    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);
    
    0 讨论(0)
  • 2020-12-07 10:30

    It's obvious, but the App Theme selection in design is just for display a draft during layout edition, is not related to real app looking in cell phone. In my case, there is no title bar in design but the title bar was always in cell phone.

    I'm starting with Android programming and fight to discover a right solution.

    Just change the manifest file (AndroidManifest.xml) is not enough because the style need to be predefined is styles.xml. Also is useless change the layout files.

    All proposed solution in Java or Kotlin has failed for me. Some of them crash the app. And if one never (like me) intend to use the title bar in app, the static solution is cleaner.

    For me the only solution that works in 2019 (Android Studio 3.4.1) is:

    in styles.xml (under app/res/values) add the lines:

       <style name="AppTheme.NoActionBar">
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>
        </style>
    

    After in AndroidManifest.xml (under app/manifests)

    Replace

    android:theme="@style/AppTheme">
    

    by

    android:theme="@style/AppTheme.NoActionBar">
    
    0 讨论(0)
  • 2020-12-07 10:33

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

      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(null);
    
    0 讨论(0)
  • 2020-12-07 10:33

    if your using default toolbar then you can add this line of code

    Objects.requireNonNull(getSupportActionBar()).setDisplayShowTitleEnabled(false);
    
    0 讨论(0)
  • 2020-12-07 10:34

    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);
    
    0 讨论(0)
提交回复
热议问题