不允许在底部应用栏中设置标题

时间:2018-07-11 10:05:13

标签: android mdc-components

我的应用程序中有一个主要活动,其中包含“材料设计组件”中的新Bottom App Bar。在用户交互中,我替换了不同的片段。

为了使用户知道他们在应用程序中的当前位置,我决定将底部应用程序栏的标题设置为与片段的标题相同。但是,我无法设置标题,并且看到实现后,发现“底部应用栏不能有标题”。

enter image description here

在这种情况下,我想知道如何使用户知道他在应用程序中的位置,或者是否有任何方法可以设置底部应用程序栏的标题。

1 个答案:

答案 0 :(得分:4)

没错,我们不能在BottomApp Bar中使用标题,因为在某些情况下(浮动按钮可能位于BottomApp Bar的中心)它不方便。

但是您的问题是真实的,我们没有应用程序栏(即使它位于底部),我们需要为此添加标题。

因此,我通过(过去的)复合布局解决了这个问题。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <include layout="@layout/content_main" />

    <com.google.android.material.bottomappbar.BottomAppBar
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        app:fabAlignmentMode="end"
        app:backgroundTint="@color/colorPrimaryDark">

        <!-- Here is the magic happens -->

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

            <TextView
                android:id="@+id/your_title"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/app_name"
                android:textColor="#fff"
                android:layout_centerInParent="true"
                android:textAppearance="@style/Base.TextAppearance.AppCompat.Title"/>

        </RelativeLayout>

    </com.google.android.material.bottomappbar.BottomAppBar>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_anchor="@id/bar"
        app:layout_insetEdge="right"
        app:rippleColor="@color/colorPrimary"
        android:src="@drawable/ic_menu_24"/>

</androidx.coordinatorlayout.widget.CoordinatorLayout> 

在上述实现中,我设置了app:fabAlignmentMode="end",以便为标题留出整洁的空间。

我们采用这种方法的好处是,您可以使用自定义字体,颜色在任何程度上自定义标题。

以上实现的输出:

Screenshot of output

我希望这可以解决您的问题。