使用底部应用栏时更改扩展浮动操作按钮的对齐方式

时间:2019-04-17 07:21:58

标签: android material-components-android

我目前正在尝试使用附加到应用程序ExtendedFloatingActionButton上的BottomAppBar,到目前为止还可以,我正面临着这样的事情:

extended floating action button alignment at center

我试图给它增加更多的感觉,所以我确实想当用户单击“存储库”按钮时,将按钮对准end而不是center并将其文本更改为{{ 1}}或null,并更改其图标。因此,我更改了代表上面图片的代码:

""

对此,我可以找出是否可能:

<androidx.coordinatorlayout.widget.CoordinatorLayout
            android:id="@+id/context_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        <com.google.android.material.bottomappbar.BottomAppBar
                android:id="@+id/bottomAppBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                app:backgroundTint="@color/primaryColor"
                app:fabAlignmentMode="center"
                app:navigationIcon="@drawable/ic_menu_24dp"/>

        <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
                android:id="@+id/repositoriesButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                app:icon="@drawable/ic_inbox_24dp"
                app:layout_anchor="@id/bottomAppBar"
                android:text="Repositories"/>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

我明白了:

Extended floating action button with no text and alignment to end

Voila!这样就可以了!

我也为此做了一些编码!我们只需要将<androidx.coordinatorlayout.widget.CoordinatorLayout android:id="@+id/context_view" android:layout_width="match_parent" android:layout_height="match_parent"> <com.google.android.material.bottomappbar.BottomAppBar android:id="@+id/bottomAppBar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" app:backgroundTint="@color/primaryColor" app:fabAlignmentMode="end" app:navigationIcon="@drawable/ic_menu_24dp"/> <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton android:id="@+id/repositoriesButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="8dp" app:icon="@drawable/ic_arrow_back_black_24dp" app:layout_anchor="@id/bottomAppBar" android:text=""/> </androidx.coordinatorlayout.widget.CoordinatorLayout> 中的fabAlignmentMode更改为BottomAppBar。所以我确实为此写了一个方法:

BottomAppBar.FAB_ALIGNMENT_MODE_END

但是结果不是我期望的!

Extended Floating Action button not aligned to end

我在private fun BottomAppBar.toggleFabAlignment() { currentFabAlignmentMode = fabAlignmentMode fabAlignmentMode = if(currentFabAlignmentMode == BottomAppBar.FAB_ALIGNMENT_MODE_CENTER) BottomAppBar.FAB_ALIGNMENT_MODE_END else BottomAppBar.FAB_ALIGNMENT_MODE_CENTER } 的源代码中进行了一些深入的搜索,并且确实发现,在更深层次上,当您想更改对齐方式时,将调用方法BottomAppBar,该方法使用了另一种方法称为createFabTranslationXAnimation(),它将在视图中搜索findDependentFab(),而不是我正在使用的fab,然后返回该值。所以我认为这个方法已经引起了问题。

我正确吗?有人建议这样做吗?

0 个答案:

没有答案