Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
setTitle(Html.fromHtml("<font color=\"white\">" + "Settings" + "</font>"));
我在 onCreate 方法上使用这两行来更改操作栏标题和颜色,但它与左侧对齐,有没有办法将其居中?
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
setTitle(Html.fromHtml("<font color=\"white\">" + "Settings" + "</font>"));
我在 onCreate 方法上使用这两行来更改操作栏标题和颜色,但它与左侧对齐,有没有办法将其居中?
而不是setTitle
使用自定义工具栏
<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);