android - 启用后退按钮时工具栏标题不在中心

标签 android android-toolbar

我试图在中心显示我的工具栏标题,为此我使用了此答案中给出的方法:- Toolbar Center title

但是,当我通过以下代码在我的 activity 中启用后退按钮时:

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

工具栏的标题没有显示在中间,而是稍微偏右。

如何在受后退按钮或菜单栏影响的情况下实现居中标题?

最佳答案

在工具栏中添加一个 TextView,不要忘记在您的 TextView 中设置以下属性。

android:layout_marginRight="?android:attr/actionBarSize"

OR

android:layout_marginEnd="?android:attr/actionBarSize"

代码 fragment :

<android.support.v7.widget.Toolbar
    android:id="@+id/custom_toolbar"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/actionBarSize"
    android:background="@android:color/holo_red_dark">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="abc"
        android:textColor="@android:color/white"
        android:textSize="20sp"
        android:layout_marginRight="?android:attr/actionBarSize"
        android:gravity="center"/>

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

引用this教程以获取更多信息。

关于android - 启用后退按钮时工具栏标题不在中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38582331/

相关文章:

android - 创建编辑文本只接受或只能输入来自 a-z 的字母字符 only android

android - 在 FrameLayout 上替换导航栏中的 ViewPager fragment 时滞后?

java - 操作栏抽屉切换无法从右侧打开导航

javascript - 错误 : Unable to resolve module `@react-native-community/toolbar-android`

android - 在 appcompat-v7 的工具栏中删除标题

android - 关闭应用程序时停止 AsyncTask

android - 同一 Activity 的多个实例?

android - 访问 AD 认证网站时出现 WebView 超时问题

android - 如何在工具栏 Android 的 SearchView 小部件中删除白色下划线

android - 协调器布局中带有可滚动工具栏的底部导航