设置Toolbar标题居中(使用TextView和直接设置Toolbar两种实现方式)

Toolbar居中是困扰我很长一段时间的问题,主要是Toolbar默认不是水平居中,这种居中还是挺好看的(支付宝就是偏左,的确很好看),但是我UI太烂,所以不居中感觉很难受
第一种
1、布局添加TextView

<?xml version="1.0" encoding="utf-8"?>
<!--AppBarLayout主要用来实现标题折叠功能elevation:阴影高度-->
<com.google.android.material.appbar.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:elevation="0dp">

    <!--Toolbar控件-->

    <androidx.appcompat.widget.Toolbar
        android:background="@color/blue"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/AppTheme.PopupOverlay" >
        <TextView
            android:textSize="20sp"
            android:textColor="@android:color/white"
            android:id="@+id/toolbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center" />
    </androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>

2、获取控件设置标题

//设置TextView的内容
toolbar_title.setText(title);
//暴力清空toolbar的默认标题,不设置的话会默认显示application的label属性值
toolbar.setTitle("");

第二种
通过获取Toolbar的标题子控件,设置子控件的对齐方式设置Toolbar居中
xml布局

<com.google.android.material.appbar.AppBarLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay"
    android:elevation="0dp">

    <!--Toolbar控件-->

    <androidx.appcompat.widget.Toolbar
        app:collapsedTitleGravity="center"
        app:contentInsetStart="0dp"
        app:contentInsetLeft="0dp"
        app:titleMargin="0dp"
        android:background="@color/blue"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:popupTheme="@style/AppTheme.PopupOverlay" >
    </androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.AppBarLayout>
	/***
     * 设置Toolbar标题居中
     */
    public void setToolbarTitleCenter() {
        String title = "title";
        final CharSequence originalTitle = toolbar.getTitle();
        toolbar.setTitle(title);
        for (int i = 0; i < toolbar.getChildCount(); i++) {
            View view = toolbar.getChildAt(i);
            if (view instanceof TextView) {
                TextView textView = (TextView) view;
                if (title.equals(textView.getText())) {
                    textView.setGravity(Gravity.CENTER);
                    Toolbar.LayoutParams params = new Toolbar.LayoutParams(Toolbar.LayoutParams.WRAP_CONTENT, Toolbar.LayoutParams.MATCH_PARENT);
                    params.gravity = Gravity.CENTER;
                    textView.setLayoutParams(params);
                }
            }
            toolbar.setTitle(originalTitle);
        }
    }

我还是喜欢第二种,但是两种都能实现。我喜欢第二种的原因是可以直接在Mainfests中设置活动的名称,需要居中的活动继承基础活动就可以实现居中效果,很方便,而如果采用第二种方式,继承自基础活动的活动还需要传值或者调用对象方法。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章