Fragment使用—侧边栏

本文介绍如何使用Fragment实现侧边栏效果,并强调在添加Fragment时使用add()方法以保存数据。通过MainActivity.java和FragmentPage1.java的代码示例,展示了Fragment的生命周期及其在添加时的区别。
摘要由CSDN通过智能技术生成

       话不多说直接看效果图和代码吧。

一、实现的效果图

二、工程结构目录

三、具体编码的实现

MainActivity的布局文件:activity_main.xml

其实很简单,左边四个按钮,右边部分用一个FrameLayout布局先把位置给占着,放Fragment

<LinearLayout 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"
    android:orientation="horizontal" >

    <LinearLayout
        android:id="@+id/ll"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:background="@drawable/maintab_toolbar_bg"
        android:orientation="vertical"
        android:gravity="center_horizontal" >

        <LinearLayout
            android:id="@+id/ll_home"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/btn_selector"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/image_home"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/home_btn_selector" />

            <TextView
                android:id="@+id/text_home"
                android:layout_width="wrap_content"
                android:layout_height="wrap_co
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值