【问题标题】:FAB doesn't stick to bottom|end when using ViewPager使用 ViewPager 时,FAB 不粘底|结束
【发布时间】:2020-05-11 21:08:37
【问题描述】:

我正在使用带有 ViewPager+TabLayout 的片段:

<androidx.constraintlayout.widget.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"
    android:orientation="vertical"
    tools:context=".ui.view.InventoryOverviewFragment">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:tabIndicatorColor="@color/colorAccent"
        app:tabMode="scrollable"
        app:tabTextColor="@android:color/white" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tabLayout" />

</androidx.constraintlayout.widget.ConstraintLayout>

ViewPager 依次提供三个不同的选项供您点击(收到的货物、售出的货物、库存水平)。其中两个片段包含一个 recyclerView 并且应该有一个 FAB(收到的货物和售出的货物)。

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.view.InventoryReceivedFragment">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerViewReceived"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:scrollbars="vertical"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/addReceived"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end|bottom"
        android:layout_margin="24dp"
        android:src="@drawable/ic_add_black_24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

当我在我的设备上测试它时,它应该在右下角,但是,FAB 总是在右上角。如代码所示,我也尝试使用约束布局,其中设置了底部和结束的约束,但没有运气......

有人可以帮我解决这个问题吗?快把我逼疯了^-^

【问题讨论】:

    标签: android layout android-constraintlayout floating-action-button


    【解决方案1】:

    您使 ViewPager 的高度与 0dp 的约束相匹配,并且您没有添加任何控制其高度的约束;所以将app:layout_constraintBottom_toBottomOf="parent" 添加到ViewPager 中,这样它就始终具有父级的整个高度,这样底层选项卡也将占据整个高度。

    如果不起作用,则将ViewPager 高度设为match_parent

    【讨论】:

    • Aaaaaah,多么愚蠢的错误 xD 现在就像一个魅力。谢谢:)
    【解决方案2】:

    你需要给 viewpager 底部约束。

    <androidx.constraintlayout.widget.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:orientation="vertical">
    
        <com.google.android.material.tabs.TabLayout
            android:id="@+id/tabLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorPrimary"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:tabIndicatorColor="@color/colorAccent"
            app:tabMode="scrollable"
            app:tabTextColor="@android:color/white" />
    
        <androidx.viewpager2.widget.ViewPager2
            android:id="@+id/viewPager"
            android:layout_width="0dp"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tabLayout" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 2017-08-14
      • 1970-01-01
      • 2017-08-06
      相关资源
      最近更新 更多