android title won't show in toolbar

2019-02-16 05:14发布

问题:

I have an xml that I use with so many activities with fragments file but my problem is that I can't display the text I want in the toolbar, I use that xml that way because I have a navigation drawer and I needed to handle somethings so I had to do it that way.

my xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/frame_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".StatusActivity"
    android:orientation="vertical" >

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        style="@style/ToolBarStyle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="@dimen/abc_action_bar_default_height_material" />

</RelativeLayout>

One of my activities:

public class GroupChatActivity extends ActionBarActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base_layout);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
         setSupportActionBar(toolbar);
         getSupportActionBar().setDisplayShowHomeEnabled(true);

         ActionBar actionBar = getSupportActionBar();
         actionBar.setTitle("Groups history");

        Aniways.init(this);

        if(savedInstanceState == null)
        {
            FragmentManager manager = getSupportFragmentManager();

            Fragment fragment = GroupChatFragment.newInstance(getIntent().getIntExtra("uid", 0));
            manager.beginTransaction().add(R.id.frame_container, fragment).commit();
        }
    }
}

as you can see I try to set title to the action bar but it doesn't work.

回答1:

getSupportActionBar().setDisplayShowTitleEnabled(true);


回答2:

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.



回答3:

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");


回答4:

Try toolbar.setTitle('Groups history');



回答5:

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

or if you are using AppCompat libraries;

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



回答6:

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

}