Android 启用“后退”按钮时,工具栏标题不在中间
我试图在中间显示我的工具栏标题,为此,我使用以下答案中给出的方法:- 但是,当我通过以下代码在我的Android 启用“后退”按钮时,工具栏标题不在中间,android,android-toolbar,Android,Android Toolbar,我试图在中间显示我的工具栏标题,为此,我使用以下答案中给出的方法:- 但是,当我通过以下代码在我的活动中启用“后退”按钮时: toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); mActionBar = getSupportActionBar(); mActionBar.setDisplayHomeAsUpEnabled(true); mActionBar.setDisplayShowHome
活动中启用“后退”按钮时:
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mActionBar = getSupportActionBar();
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setDisplayShowHomeEnabled(true);
工具栏的标题不显示在中间,但向右稍微偏离中心
如何在不受后退按钮或菜单栏影响的情况下实现居中标题?不要设置这样的属性
mActionBar = getSupportActionBar();
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setDisplayShowHomeEnabled(true);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Title and subtitle
toolbar.setTitle(R.string.about_toolbar_title);
toolbar.setSubtitleTextColor(Color.WHITE);
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setBackgroundColor(getResources().getColor(
R.color.themeToolbarColor));
toolbar.setNavigationIcon(R.drawable.ic_action_back);
toolbar.setNavigationOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish(); // to go back finish() will do your work.
//mActionBar.setDisplayHomeAsUpEnabled(true);
//mActionBar.setDisplayShowHomeEnabled(true);
}
});
你喜欢这样吗
mActionBar = getSupportActionBar();
mActionBar.setDisplayHomeAsUpEnabled(true);
mActionBar.setDisplayShowHomeEnabled(true);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// Title and subtitle
toolbar.setTitle(R.string.about_toolbar_title);
toolbar.setSubtitleTextColor(Color.WHITE);
toolbar.setTitleTextColor(Color.WHITE);
toolbar.setBackgroundColor(getResources().getColor(
R.color.themeToolbarColor));
toolbar.setNavigationIcon(R.drawable.ic_action_back);
toolbar.setNavigationOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish(); // to go back finish() will do your work.
//mActionBar.setDisplayHomeAsUpEnabled(true);
//mActionBar.setDisplayShowHomeEnabled(true);
}
});
在工具栏中添加TextView&别忘了在TextView中设置以下属性
android:layout_marginRight=“?android:attr/actionBarSize”
或
android:layout_margined=“?android:attr/actionBarSize”
代码段:
<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>
教程了解更多信息。只需将android:paddingnd=“72dp;”
添加到工具栏布局中即可
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
app:contentScrim="@color/colorPrimary"
app:layout_collapseMode="pin"
android:paddingEnd="72dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"
app:title="Title"/>
有一个与后向箭头大小相同的占位符图像,当后向箭头未显示时将其设置为不可见,当后向箭头显示时将其设置为不可见,这对我来说很有帮助
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/blue"
android:minHeight="?attr/actionBarSize"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<ImageView
android:id="@+id/iv_placeholder"
android:layout_width="72dp"
android:layout_height="48dp"
android:src="@drawable/ic_actionbar_hamburger"
android:visibility="invisible"/>
<TextView
android:id="@+id/logo_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="?attr/actionBarSize"
android:gravity="center"
android:text=""
android:textColor="@color/white"
android:textStyle="normal"/>
</android.support.v7.widget.Toolbar>
当您使用后退按钮作为导航图标时,标题不居中的原因是导航图标表示为AppCompatImageButton
,并添加到与标题TextView
相同的布局中。使用不是一个坏主意,但是?android:attr/actionBarSize
不是定义结尾的好方法边距。由于操作栏的高度可能与图标的宽度相同,因此它可能会起作用,但可能不会在所有设备上都起作用。从中指定此大小可能是一个好主意。只需使用以下属性,将您的内容放在XML工具栏标记内的子视图中即可:
android:layout_width="wrap_content"
android:layout_gravity="center"
工具栏状态的官方文档:
一个或多个自定义视图。应用程序可能会将任意子视图添加到工具栏中。它们将显示在布局中的此位置。如果子视图的LayoutParams指示重力值为水平#居中#,则在所有其他元素均已添加后,视图将尝试在工具栏中剩余的可用空间内居中测量
这对我来说很有效,使用androidx.appcompat.widget.Toolbar和子视图。将margined或paddinged改为72dp,准确地说是64dp。我最近发现仔细检查android:layout\u margined=“?android:attr/actionBarSize”也很有效。你需要使用android:layout\u gravity=“center”
&android:layout\u width=”为TextView
包装内容“
。不需要android:layout\u marginRight
或其他东西。@如果这样做,您会注意到,当“后退”按钮可见时,文本稍微向右定位,而不是完全居中(因为它从可用空间居中).所以,这只是一个解决办法。也许我是瞎子,但这不是真的。@YaroslavOvdiienko hahaha.我刚在Android studio上试用过,它仍然在左边留出空间。你为什么不发布一个答案呢?也许这可能会对某人有所帮助。