Android Studio 开发之——电子阅览器(一)
说明:此博客主要用于讲解简单的电子阅览器的实现,本篇主要说明菜单界面的实现与代码
菜单界面框架
菜单界面采用tablayout+viewpager2+fragment
实现,实现可滑动的菜单页面效果。
我们先来看主界面布局,mainactivity.xml
如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewpage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@+id/rg_tab_bar" />
<com.google.android.material.tabs.TabLayout
android:id="@+id/rg_tab_bar"
app:tabIndicatorHeight="3dp"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_alignParentBottom="true">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="书架" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="分类" />
</com.google.android.material.tabs.TabLayout>
<View
android:id="@+id/div_tab_bar"
android:layout_width="match_parent"
android:layout_height="2px"
android:layout_above="@id/rg_tab_bar"
android:background="@color/div_white" />
<FrameLayout
android:id="@+id/ly_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/div_tab_bar"/>
</RelativeLayout>
上述代码中,采用相对布局的方式,在底部使用tablayout
控件导航栏,导航栏以上部分为fragment
,展示具体一个页面内容,使用viewpager2
实现多个页面间的滑动。
MainActivity.java
代码如下
package com.example.ebooktest;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager2.widget.ViewPager2;
import androidx.fragment.app.Fragment