平铺没有在Collapsed工具栏中获得中心

时间:2017-11-14 12:44:17

标签: android android-toolbar android-collapsingtoolbarlayout

我尝试设置collapsingToolbarLayout.setCollapsedTitleGravity(Gravity.CENTER);

collapsingToolbarLayout.setExpandedTitleGravity(Gravity.CENTER);

以及INTERNET上的几乎所有可用链接,但我无法将标题置于toolbar的中心。

enter image description here

plz help!

2 个答案:

答案 0 :(得分:1)

要使工具栏中的文本居中,您需要使用其中包含textview的自定义工具栏,而不是使用android:title =“”

<android.support.v7.widget.Toolbar
    android:layout_width="match_parent"
    android:id="@+id/toolbar"
    android:layout_height="?actionBarSize">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text=""
        android:textColor="#000000"
        android:id="@+id/tvToolbarTitle"
        android:layout_gravity="center"
        android:textSize="16sp"/>

</android.support.v7.widget.Toolbar>

您可以在可折叠工具栏中应用相同的逻辑。

答案 1 :(得分:1)

让你的Toolbar像这样

       <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:contentInsetEnd="0dp"
            app:contentInsetLeft="0dp"
            app:contentInsetRight="0dp"
            app:contentInsetStart="0dp"
            app:contentInsetStartWithNavigation="0dp"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="vertical"
                android:text="Center">

                <TextView
                    android:layout_marginLeft="-25dp"
                    android:id="@+id/toolbar_title"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="center" />
            </LinearLayout>

        </android.support.v7.widget.Toolbar>
活动文件中的

会像这样改变

toolbar=(Toolbar)findViewById(R.id.tool_bar);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
mTitle.setText("PREM");
setSupportActionBar(toolbar);