android title won't show in toolbar

后端 未结 9 1510
粉色の甜心
粉色の甜心 2021-01-07 16:41

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

相关标签:
9条回答
  • 2021-01-07 16:54

    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
    
    }
    
    0 讨论(0)
  • 2021-01-07 16:58

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

    or if you are using AppCompat libraries;

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

    0 讨论(0)
  • 2021-01-07 16:59
    getSupportActionBar().setDisplayShowTitleEnabled(true);
    
    0 讨论(0)
  • 2021-01-07 17:12

    if your title color is white and the toolbar is also white, you will not differentiate it. So try to change the toolbar color

    0 讨论(0)
  • 2021-01-07 17:14

    I actually had to get the toolbar_title to set the text into each different activity:

        toolbar = findViewById(R.id.toolbar);
        toolbarTitle = findViewById(R.id.toolbar_title);   //<----- here
        toolbarTitle.setText(getString(R.string.my_activity_toolbar_title));
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
    0 讨论(0)
  • 2021-01-07 17:15

    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.

    0 讨论(0)
提交回复
热议问题