一、基础实现步骤
1. 添加依赖
在 app/build.gradle中添加依赖:
dependencies {
implementation 'androidx.viewpager2:viewpager2:1.0.0'
implementation 'com.google.android.material:material:1.4.0' // 用于 TabLayout
}
2. 布局文件 (activity_main.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- TabLayout 顶部导航 -->
<com.google.android.material.tabs.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<!-- ViewPager2 滑动区域 -->
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent"

最低0.47元/天 解锁文章
435

被折叠的 条评论
为什么被折叠?



