BottomAppBar忽略“ layout_gravity”,始终显示在顶部

时间:2018-06-23 22:21:19

标签: android android-layout

我正在尝试在我的应用中实现BottomAppBar,但是无法在底部显示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>

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

Image

如何在底部对齐?

4 个答案:

答案 0 :(得分:1)

有时也取决于您的根目录布局。
对于ConstraintLayout,请尝试app:layout_constraintBottom_toBottomOf="parent"

也请检查此answer是否有其他布局。

答案 1 :(得分:1)

ConstraintLayout不使用layout_gravity。而是使用:

var interval;
function beginTimer() {
  clearInterval(interval);
  interval = setInterval(function(){
...

没有保证金。

答案 2 :(得分:1)

首先,将布局更改为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>

尝试一下,这样对您来说就可以了

答案 3 :(得分:0)

您应该尝试以下操作:

<?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.bottomappbar.BottomAppBar
        android:id="@+id/bottom_bar"
        android:layout_width="0dp"
        android:layout_height="@dimen/mtrl_bottomappbar_height"
        app:backgroundTint="@color/colorPrimary"
        app:fabAlignmentMode="center"
        app:fabAttached="true"
        app:fabCradleVerticalOffset="12dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</android.support.constraint.ConstraintLayout>