首页 > 解决方案 > BottomAppBar 忽略“layout_gravity”,始终显示在顶部

问题描述

我正在尝试在BottomAppBar我的应用程序中实现 a 并且无法制作 FAB 或BottomAppBar在底部显示。这是我的布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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=".dashboard.DashboardActivity">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_add_white_24dp"
    app:layout_anchor="@id/bottom_bar"/>

<android.support.design.bottomappbar.BottomAppBar
    android:id="@+id/bottom_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/mtrl_bottomappbar_height"
    android:layout_gravity="bottom"
    app:backgroundTint="@color/colorPrimary"
    app:fabAttached="true"
    app:fabCradleVerticalOffset="12dp"
    app:fabAlignmentMode="center"/>

</android.support.constraint.ConstraintLayout>

但是,这就是模拟器中显示的内容:

图片

如何在底部对齐?

标签: androidandroid-layout

解决方案


首先,将布局更改为 LinearLayout 并将方向定义为垂直,然后将您的代码放在另一个 LinearLayout 中,您可以在其中设置它的 marginTop ,因为您的要求只是进行这些更改它应该可以正常工作

<LinearLayout
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=".dashboard.DashboardActivity"
android:orientation="vertical"

>

<LinearLayout
   android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_marginTop="330dp" //mark height accordingly
 >


  \\put your code here



</LinearLayout>


</LinearLayout>

试试这个应该适合你


推荐阅读