Tile not getting center in Collapsed toolbar

别来无恙 提交于 2019-12-20 07:19:04

问题


I tried setting collapsingToolbarLayout.setCollapsedTitleGravity(Gravity.CENTER);

collapsingToolbarLayout.setExpandedTitleGravity(Gravity.CENTER);

and almost all available links on INTERNET but i am not able to put the title in center of toolbar.

plz help !


回答1:


make your Toolbar like this

       <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>

in your activity file change like this

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



回答2:


To center the text in the toolbar you need to use a custom toolbar with a textview inside it rather than using 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>

You can apply the same logic in a collapsible toolbar.



来源:https://stackoverflow.com/questions/47286252/tile-not-getting-center-in-collapsed-toolbar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!