首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ToolBar的中心标题

ToolBar的中心标题
EN

Stack Overflow用户
提问于 2015-05-08 08:50:26
回答 2查看 1.8K关注 0票数 1

我有一个问题,我不能把我的标题在我的工具栏(@+id/工具栏_ title )。我试着放置一个RelativeLayout,使用layout_gravity: center,但是它不起作用。我增加了颜色来区分布局,但我还不能发布图片。

http://hpics.li/72b1cf7

activity_main.xml

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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>
EN

回答 2

Stack Overflow用户

发布于 2015-05-08 15:00:34

在工具栏中以文本为中心是违反Android设计标准的。我想这就是为什么你在完成这件事上有问题。我不会试图调整您的文本对齐,这将确保您的应用程序符合Android生态系统。

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

我没有回答你会如何做你想做的事情,但是建议你一开始就不要这样做(使用标准的格式,这对你来说会更容易)。

票数 1
EN

Stack Overflow用户

发布于 2015-05-08 10:53:41

试试这个密码..。

工具栏的父视图是视图组。所以你不能那样设计。我使用FrameLayout来放置物品。

代码语言:javascript
复制
               <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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30119622

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档