我试图在中心显示我的工具栏标题,为此我使用了此答案中给出的方法:- 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/