【问题标题】:Center action bar title - Android中心操作栏标题 - Android
【发布时间】:2021-12-15 11:34:21
【问题描述】:
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
setTitle(Html.fromHtml("<font color=\"white\">" + "Settings" + "</font>"));

This is how the action bar looks like now

我在 onCreate 方法中使用这两行来更改操作栏的标题和颜色,但它是靠左对齐的,有没有办法让它居中?

【问题讨论】:

    标签: android android-actionbar center-align


    【解决方案1】:

    不要使用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);
    

    【讨论】:

    • 其实我有点想在这两行中寻找一种方法来解决这个问题,而不是去定制一个,不过还是谢谢