android title won't show in toolbar

佐手、 提交于 2019-12-01 02:06:21
getSupportActionBar().setDisplayShowTitleEnabled(true);

Setting,

app:title="@string/my_title"

within the declaration of the the android.support.v7.widget.Toolbar, hard codes the title in the toolbar.

To set the title programatically,

Toolbar toolbar = (Toolbar) findViewById(R.id.my_toolbar);
toolbar.setTitle("my title");
setSupportActionBar(toolbar);

in your activity class.

Try this .. this method works for me..!! hope it may help somebody..!!

<android.support.v7.widget.Toolbar  
 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" >  

<TextView  
   android:id="@+id/toolbar_title"  
   android:layout_width="wrap_content"  
   android:layout_height="wrap_content"  
   android:layout_gravity="center"  
   android:singleLine="true"  
   android:text="Toolbar Title"  
   android:textColor="@android:color/white"  
   android:textSize="18sp"  
   android:textStyle="bold" />  

</android.support.v7.widget.Toolbar>  

EDIT

You can also use this.

setSupportActionBar(toolbar);
if (getSupportActionBar() != null)
      getSupportActionBar().setTitle("Toolbar title");
csmurphy84

Try toolbar.setTitle('Groups history');

Kyle Mew

getActionBar().setTitle("Groups History");

or if you are using AppCompat libraries;

getSupportActionBar().setTitle("Groups History");

I did a custom action bar.

Layout iguepardos_action_bar.xml with this code

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blanco"
android:minHeight="?attr/actionBarSize">

    <TextView
    android:id="@+id/toolbar_title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="left"
    android:singleLine="true"
    android:text="Toolbar Title"
    android:textColor="@color/naranja"
    android:textSize="18sp" />

</android.support.v7.widget.Toolbar>

In my Class extended AppCompatActivity I had this:

protected void onCreate(Bundle savedInstanceState) {
.... 
....

getSupportActionBar().setDisplayShowCustomEnabled(true); // is for specify use custom bar 
getSupportActionBar().setCustomView(R.layout.iguepardos_action_bar);  // my custom layout
getSupportActionBar().setDisplayHomeAsUpEnabled(true); // Button for come back

View mCustomView = getSupportActionBar().getCustomView(); // Set the view
TextView TitleToolBar = (TextView) mCustomView.findViewById(R.id.toolbar_title); // find title control
TitleToolBar.setText("The Title Show"); // Set the Title

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!