【问题标题】:Setting title in the Bottom App Bar not allowed不允许在底部应用栏中设置标题
【发布时间】:2018-07-11 10:05:12
【问题描述】:

我的应用中有一个主要活动,其中包含来自 Material Design Components 的新 Bottom App Bar。在用户交互时,我替换了不同的片段。

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

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

【问题讨论】:

标签: android mdc-components


【解决方案1】:

是的,我们不能在 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",这样我们就可以为标题留出整洁的空间。

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

上述实现的输出:

我希望这能解决你的问题。

【讨论】:

    猜你喜欢
    • 2019-07-13
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 2019-04-24
    • 2011-06-28
    • 2020-06-09
    • 2018-11-18
    • 1970-01-01
    相关资源
    最近更新 更多