微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使 AppCompatActivity 活动可滑动并包含应用栏?

如何解决如何使 AppCompatActivity 活动可滑动并包含应用栏?

我共有三个类型为 AppCompatActivity 的活动。在每个活动的 xml 中,我有以下内容来设置应用栏:

<com.google.android.material.appbar.AppBarLayout
      android:id="@+id/app_bar_layout"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:visibility="invisible"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toTopOf="@+id/view">

    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:visibility="invisible"
        app:tabBackground="@android:color/white"
        app:tabGravity="fill"
        app:tabInlineLabel="true"
        app:tabMode="fixed"
        app:tabTextAppearance="@style/CustomTabStyle"
        app:tabTextColor="@android:color/black" />
</com.google.android.material.appbar.AppBarLayout>

我实现了这个应用栏,允许用户通过点击应用栏中的三个按钮之一来更改活动。如果用户更愿意浏览活动,我会在每个活动的 xml 中包含以下内容

<androidx.viewpager2.widget.ViewPager2
      android:id="@+id/view_pager"
      android:layout_width="0dp"
      android:layout_height="0dp"
      android:visibility="invisible"
      app:layout_constraintBottom_toBottomOf="parent"
      app:layout_constraintEnd_toEndOf="parent"
      app:layout_constraintStart_toStartOf="parent"
      app:layout_constraintTop_toBottomOf="@+id/app_bar_layout" />

我现在遇到的问题是我不能将 ViewPager2 用于 AppCompatActivity。它只能用于 Fragments,但这不适用于我已有的代码。我如何使 AppCompatActivity 类型的三个活动可滑动并在每个活动中包含一个应用栏,以便用户可以点击按钮转到新活动?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。