Center title on ToolBar

◇◆丶佛笑我妖孽 提交于 2020-01-14 04:38:05

问题


I have a problem, I can't center my title in my Toolbar (@+id/toolbar_title). I tried to put a RelativeLayout, use layout_gravity: center but it doesn't work. I have added colors to distinguish the layout but I can't post pictures yet.

http://hpics.li/72b1cf7

activity_main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".MainActivity"

    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="60dp"
                android:background="@android:color/holo_green_dark"
                app:theme="@style/ToolbarTheme"
                app:popupTheme="@style/Theme.AppCompat"
                android:layout_gravity="center_vertical"
                >

                <TextView
                    android:id="@+id/toolbar_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerVertical="true"
                    android:layout_gravity="center"
                    android:background="@color/primaryDark"
                    android:textSize="20dp"
                    />

                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@color/primary">

                    <ImageButton
                        android:id="@+id/show_list_actualites"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_fleche_bas"
                        android:adjustViewBounds="true"
                        android:layout_toLeftOf="@+id/loupe"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="15dp"
                        android:layout_centerVertical="true"
                        android:padding="15dp"
                        android:background="@color/primaryDark"
                        />
                    <!--android:background="@android:color/transparent"-->

                  <ImageView
                        android:id="@+id/loupe"
                        android:layout_width="wrap_content"
                        android:layout_height="12dip"
                        android:src="@drawable/ic_rech"
                        android:rotation="270"
                        android:adjustViewBounds="true"
                        android:layout_centerVertical="true"
                        android:layout_alignParentRight="true"
                        android:layout_marginRight="20dp"
                        android:background="@color/primaryDark"
                        />

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

        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <FrameLayout
                android:id="@+id/content_fragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>

            <LinearLayout
                android:id="@+id/slide_news_list"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:visibility="gone">

                <ListView
                    android:id="@+id/list_slider_news_menu"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/action_bar_background"
                    android:text="Item 1"/>


                <Button
                    android:id="@+id/close_news_slide"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:background="@color/grey_tranparent"/>

            </LinearLayout>

        </RelativeLayout>

    </LinearLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:background="@android:color/white"
        android:layout_width="305dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"/>

</android.support.v4.widget.DrawerLayout>

styles.xml

<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="Theme.AppCompat.Light.NoActionBar" parent="@style/Theme.AppCompat.Light">
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primaryDark</item>
    </style>

    <style name="AppTheme" parent="AppTheme.Base">

    </style>

    <style name="ToolbarTheme" parent="Theme.AppCompat">
        <item name="android:windowNoTitle">true</item>
        <item name="android:textColorPrimary">@color/action_bar_text</item>
        <item name="actionMenuTextColor">@color/action_bar_text</item>
        <item name="android:textColorSecondary">@color/action_bar_text</item>
        <item name="android:layout_gravity">center_horizontal</item>
    </style>

</resources>

回答1:


Centering the Text in the Toolbar is against Android design standards. I think this is why you are having issues accomplishing this. I would not try to adjust your text alignment, which will insure your app fits within the Android ecosystem.

Check this link for details on Toolbar design standards: http://www.google.com/design/spec/layout/structure.html#structure-toolbars

I am not answering how you WOULD do what you are asking, but suggesting that you DON'T do it in the first place (use the standard format, and this will be easier for you).




回答2:


try this code...

Toolbar's parent is viewgroup. So u cannot design like that. I used FrameLayout to place items.

               <FrameLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">



                <RelativeLayout
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent"
                    android:background="@color/primary">

                    <ImageButton
                        android:id="@+id/show_list_actualites"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:src="@drawable/ic_launcher"
                        android:adjustViewBounds="true"
                        android:layout_toLeftOf="@+id/loupe"
                        android:layout_marginLeft="20dp"
                        android:layout_marginRight="15dp"
                        android:layout_centerVertical="true"
                        android:padding="15dp"
                        android:background="@color/primaryDark"
                        />
                    <!--     android:background="@android:color/transparent"-->

                    <ImageView
                        android:id="@+id/loupe"
                        android:layout_width="wrap_content"
                        android:layout_height="12dip"
                        android:src="@drawable/ic_launcher"
                        android:rotation="270"
                        android:adjustViewBounds="true"
                        android:layout_centerVertical="true"
                        android:layout_alignParentRight="true"
                        android:layout_marginRight="20dp"
                        android:background="@color/primaryDark"
                        />

                </RelativeLayout>

                    <TextView
                        android:id="@+id/toolbar_title"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_centerVertical="true"
                        android:layout_gravity="center"
                        android:background="@color/primaryDark"
                        android:textSize="20dp"
                        android:text="Title"
                        />

                </FrameLayout>


来源:https://stackoverflow.com/questions/30119622/center-title-on-toolbar

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