工具栏上的中心标题 [英] Center title on ToolBar

查看:72
本文介绍了工具栏上的中心标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,我无法将标题放在工具栏中(@ + id/toolbar_title).我试图放一个RelativeLayout,使用layout_gravity:center,但是不起作用.我添加了颜色来区分布局,但是我还不能发布图片.

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>

推荐答案

在工具栏中居中放置文本违反了Android设计标准.我认为这就是您在实现此目标时遇到问题的原因.我不会尝试调整您的文本对齐方式,以确保您的应用适合Android生态系统.

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.

检查此链接以获取有关工具栏设计标准的详细信息: http: //www.google.com/design/spec/layout/structure.html#structure-toolbars

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

这篇关于工具栏上的中心标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆