Android Studio 开发之——电子阅览器(一)

这篇博客介绍了如何在Android Studio中开发一个电子阅览器的菜单界面,重点讲解了使用ViewPager实现可滑动菜单页面,通过设置Adapter与TabLayout关联,确保页面滑动时标题栏同步更新。步骤包括布局设计、ViewPager与Fragment的结合以及Adapter的实现。
摘要由CSDN通过智能技术生成

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
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值