android - FloatingActionButton 在 ViewPager 中不可见

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

我有一个 ViewPager,里面有一个在屏幕上不可见的 FloatingActionButton。 在 ActivityMain 文件中我没有采取任何行动。 这是我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" >

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/floatingActionButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:src="@android:drawable/arrow_up_float"
            android:focusable="true"
            app:layout_anchor="@id/viewpager"/>

    </androidx.viewpager.widget.ViewPager>
</androidx.constraintlayout.widget.ConstraintLayout>

最佳答案

将你的 FloatingActionButton 移到 ViewPager 之外

试试这个

<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"
    tools:context=".MainActivity">

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" >


    </androidx.viewpager.widget.ViewPager>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/floatingActionButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:clickable="true"
        android:focusable="true"
        android:src="@android:drawable/arrow_up_float"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toBottomOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

关于android - FloatingActionButton 在 ViewPager 中不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57710722/

相关文章:

android - 向每个 ViewPager 幻灯片添加多个图像和文本

android - 为什么我应该使用 FragmentPagerAdapter 而不是 FragmentStatePagerAdapter?

Android 开发 - getMenuInflater(R.menu.main, menu)

Android循环更新TextView

android - 在android中是否有任何直接执行查询并返回游标的方法?

java - 在android中处理多个 View

Android 使用 Intent 向日历添加事件,获取 EventID

java - AdjustPan 在某些布局中不起作用

android - 单选按钮仅被部分选中

Android:覆盖方法时出错