Android 中导航栏文字居中

Android 中导航栏文字居中

1. 隐藏原来的导航栏

在这里插入图片描述
在这里插入图片描述

        <activity
            android:name=".MainActivity"
            android:label="@string/main_title"
            android:theme="@style/CustomMainTheme" />
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!-- Main 首页的头部样式, 先隐藏后添加 -->
    <style name="CustomMainTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
        <item name="android:background">@color/main_header</item>
    </style>

</resources>

2. 添加导航栏

先建立一个布局: layout_main_actionbar.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="40dp">

    <TextView
        android:id="@+id/main_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/main_title"
        android:textColor="@color/white" />

</RelativeLayout>

嵌入这个布局

在这里插入图片描述

是标题居中

在这里插入图片描述

        // 标题栏居中
        TextView tv_title = findViewById(R.id.main_title);
        tv_title.setGravity(Gravity.CENTER);

猜你喜欢

转载自blog.csdn.net/YKenan/article/details/114133358