【问题标题】:Center Toolbar Title in custom toolbar with icons and navigation drawer toggle button自定义工具栏中的中心工具栏标题,带有图标和导航抽屉切换按钮
【发布时间】:2017-09-04 11:48:24
【问题描述】:

我正在尝试在带有导航抽屉的应用中实现工具栏。现在我试图将我的工具栏的标题放在所有片段的中心。我的问题是,有时我在导航抽屉的汉堡图标右侧有一个图标,而我在工具栏左侧有一个应用程序图标。为了使这些额外的图标可见,我必须使用相对布局,否则左侧的应用程序图标将丢失。

我的带有工具栏的 Activity 的 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"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
tools:context="app.activities.HomeActivity">

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

    <android.support.v7.widget.Toolbar
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/home_toolbar"
        android:minHeight="?attr/actionBarSize"
        android:background="@color/white"
        app:contentInsetStartWithNavigation="0dp">

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

            <ImageView
                android:id="@+id/search_toolbar_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="8dp"
                android:paddingRight="8dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:src="@drawable/icon_search_hightlighted" />

            <ImageView
                android:id="@+id/refresh_toolbar_icon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="8dp"
                android:paddingRight="8dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:src="@drawable/icon_refresh"
                android:visibility="gone"/>

            <TextView
                android:id="@+id/actionbar_title"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:text="@string/action_bar_title_home"
                android:gravity="center"
                android:textColor="@color/black"
                android:textSize="18sp" />

            <ImageView
                android:id="@+id/logo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:scaleType="centerCrop"
                android:layout_alignParentRight="true"
                android:src="@drawable/app_logo" />

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


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

<fragment
    android:id="@+id/navigation_drawer"
    android:name="app.fragments.NavigationDrawerFragment"
    android:layout_width="@dimen/navigation_drawer_width"
    android:layout_marginTop="?attr/actionBarSize"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    tools:layout="@layout/fragment_navigation_drawer" />

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

我将左侧工具栏图标的可见性设置为消失或可见,具体取决于所选片段。 有谁知道如何将工具栏标题居中并在我的工具栏中仍然可以看到我的 ImageView?

提前致谢。

编辑

我的工具栏标题目前没有居中。它的位置类似于这个问题: ToolBar title doesn't center

我已经读过,如果相对布局消失并将 TextView 的布局重力设置为 center_horizo​​ntal ,则可以将其居中,但这对我不起作用,因为我的应用程序的 ImageView 不会t 出现在工具栏的右侧。

【问题讨论】:

    标签: android android-fragments navigation-drawer android-toolbar


    【解决方案1】:

    我没有足够的声望来发表评论。

    你的问题对我来说有点不清楚。如果我理解正确,您已经设法将标题居中,但是当您将 imageViews 的可见性设置为消失时,标题不再居中?

    如果理解正确,您是否尝试将 imageViews 的可见性设置为不可见而不是消失?这样做时,imageView 仍会占用布局中的相同空间,但不会对用户可见。

    【讨论】:

    • 我编辑了这个问题并添加了一个指向另一个问题的链接,这有点显示我的标题是如何定位的。它在中心的右侧。
    • 您是否尝试过使用 FrameLayout 代替它?也许这将为您提供所需的控制,使用您提到的方法,您可以使用但右侧的图像消失了。
    【解决方案2】:

    toolbar 尝试以下属性。

    <android.support.v7.widget.Toolbar
        xmlns:app="schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/primaryColor"
        android:contentInsetLeft="0dp"
        android:contentInsetStart="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        android:contentInsetRight="0dp"
        android:contentInsetEnd="0dp"
        app:contentInsetRight="0dp"
        app:contentInsetEnd="0dp" />
    

    【讨论】:

    • 当我添加此代码时,我的相对布局与整个工具栏匹配(以前没有的),并且标题在预览中看起来居中,但不在应用程序中。
    • 你的意思是没有看到app的变化?
    【解决方案3】:

    我现在就这样解决了:

    <android.support.v7.widget.Toolbar
        android:id="@+id/home_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:layout_gravity="center"
        android:background="@color/white"
        app:contentInsetEnd="0dp"
        app:contentInsetLeft="0dp"
        app:contentInsetRight="0dp"
        app:contentInsetStart="0dp"
        app:contentInsetStartWithNavigation="0dp">
    
        <ImageView
            android:id="@+id/search_toolbar_icon"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:src="@drawable/icon_search_hightlighted" />
    
        <TextView
            android:id="@+id/actionbar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/action_bar_title_home"
            android:textColor="@color/black"
            android:textSize="18sp" />
    
        <ImageView
            android:id="@+id/logo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            android:adjustViewBounds="true"
            android:scaleType="centerCrop"
            android:src="@drawable/nav_icon"
            android:layout_marginEnd="10dp"
            android:layout_marginRight="10dp" />
    
        <ImageView
            android:id="@+id/refresh_toolbar_icon"
            android:layout_width="36dp"
            android:layout_height="36dp"
            android:padding="8dp"
            android:src="@drawable/icon_refresh"
            android:visibility="gone"
            tools:visibility="visible" />
    
    </android.support.v7.widget.Toolbar>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多