简体   繁体   中英

Center action bar title - Android

Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
setTitle(Html.fromHtml("<font color=\"white\">" + "Settings" + "</font>"));

This is how the action bar looks like now

I'm using these two lines on onCreate method to change action bar title and color but it's aligned to the left, is there a way to center it?

Instead of using setTitle use a custom toolbar

<android.support.v7.widget.Toolbar
     android:id="@+id/toolbar_top"
     android:layout_height="wrap_content"
     android:layout_width="match_parent"
     android:minHeight="?android:attr/actionBarSize"
     android:background="@color/action_bar_bkgnd"
     app:theme="@style/ToolBarTheme" >


 <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Toolbar Title"
    android:layout_gravity="center"
    android:id="@+id/toolbar_title" />


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



//In your activity file
   Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_top);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM