MaterialToolbar的app:titleCentered属性不可用,如何实现标题居中?
MaterialToolbar标题居中问题的解决方法
方法一:确保使用Material Components主题
app:titleCentered="true"是Material Components库专属属性,需要App主题继承自Material系列主题才能生效。检查res/values/styles.xml中的App主题:
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar"> <!-- 其他主题配置 --> </style>
确认主题后,直接在你的MaterialToolbar中添加该属性:
<com.google.android.material.appbar.MaterialToolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/idToolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:contentInsetStartWithNavigation="0dp" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:titleTextAppearance="@style/ToolbarFontStyle" app:titleCentered="true" />
方法二:自定义Toolbar布局(兼容所有场景)
若主题调整后仍无效,或需要更灵活的标题样式,可直接自定义Toolbar内部布局:
- 创建
layout/custom_toolbar.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:orientation="horizontal" android:gravity="center_vertical"> <!-- 可选:返回按钮 --> <ImageView android:id="@+id/btn_back" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="16dp" android:src="@drawable/ic_back" /> <!-- 居中标题 --> <TextView android:id="@+id/tv_title" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="标题" android:textColor="@android:color/white" android:textSize="18sp" android:textStyle="bold" /> <!-- 可选:右侧按钮 --> <ImageView android:id="@+id/btn_menu" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="16dp" android:src="@drawable/ic_menu" /> </LinearLayout>
- 在原布局中引用该自定义布局:
<com.google.android.material.appbar.MaterialToolbar xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/idToolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:contentInsetStartWithNavigation="0dp" app:popupTheme="@style/ThemeOverlay.AppCompat.Light" app:titleTextAppearance="@style/ToolbarFontStyle" app:layout="@layout/custom_toolbar" />
- 代码中设置标题:
// Kotlin findViewById<TextView>(R.id.tv_title).text = "你的标题" // Java TextView tvTitle = findViewById(R.id.tv_title); tvTitle.setText("你的标题");
方法三:通过代码动态设置标题居中
无需修改布局或主题,直接通过代码调整标题TextView的对齐方式:
// Kotlin val toolbar = findViewById<MaterialToolbar>(R.id.idToolbar) for (view in toolbar.children) { if (view is TextView && view.text == toolbar.title) { view.gravity = Gravity.CENTER val params = view.layoutParams as Toolbar.LayoutParams params.width = ViewGroup.LayoutParams.MATCH_PARENT view.layoutParams = params break } } // Java MaterialToolbar toolbar = findViewById(R.id.idToolbar); for (int i = 0; i < toolbar.getChildCount(); i++) { View view = toolbar.getChildAt(i); if (view instanceof TextView && ((TextView) view).getText().equals(toolbar.getTitle())) { ((TextView) view).setGravity(Gravity.CENTER); Toolbar.LayoutParams params = (Toolbar.LayoutParams) view.getLayoutParams(); params.width = ViewGroup.LayoutParams.MATCH_PARENT; view.setLayoutParams(params); break; } }
内容的提问来源于stack exchange,提问作者Yasya El Hakim
相关产品推荐
相关产品推荐

