Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Android使用Fragment仿微信底部导航栏

Android使用Fragment仿微信底部导航栏

作者头像
夜雨飘零
修改于 2020-05-21 13:40:46
修改于 2020-05-21 13:40:46
1.9K0
举报
文章被收录于专栏:CSDN博客CSDN博客

原文博客:Doi技术团队 链接地址:https://blog.doiduoyi.com/authors/1584446358138 初心:记录优秀的Doi技术团队学习经历

这是一个使用Fragment做的一个底部导航栏的小demo

MainActivity的代码

代码语言:txt
AI代码解释
复制
package com.example.dell.myapplication;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageButton weixin;
    private ImageButton contact;
    private ImageButton find;
    private ImageButton me;
    private ContactFragment contactFragment;
    private WeiXinFragment weiXinFragment;
    private FindFragment findFragment;
    private MeFragment meFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        weixin = (ImageButton) findViewById(R.id.weixin1);
        contact = (ImageButton) findViewById(R.id.weixin2);
        find = (ImageButton) findViewById(R.id.weixin3);
        me = (ImageButton) findViewById(R.id.weixin4);

        weixin.setOnClickListener(this);
        contact.setOnClickListener(this);
        find.setOnClickListener(this);
        me.setOnClickListener(this);

        //该按钮点击一次
        weixin.performClick();
    }

    @Override
    public void onClick(View v) {
        cleanIcn();
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        switch (v.getId()){
            case R.id.weixin1:
                if (weiXinFragment == null) {
                    weiXinFragment = new WeiXinFragment();
                }
                transaction.replace(R.id.fragment_container,weiXinFragment);
                weixin.setImageResource(R.drawable.weixin2);
                break;
            case R.id.weixin2:
                if (contactFragment == null) {
                    contactFragment = new ContactFragment();
                }
                transaction.replace(R.id.fragment_container,contactFragment);
                contact.setImageResource(R.drawable.weixin2);
                break;
            case R.id.weixin3:
                if (findFragment == null) {
                    findFragment = new FindFragment();
                }
                transaction.replace(R.id.fragment_container,findFragment);
                find.setImageResource(R.drawable.weixin2);
                break;
            case R.id.weixin4:
                if (meFragment== null){
                    meFragment = new MeFragment();
                }
                transaction.replace(R.id.fragment_container,meFragment);
                me.setImageResource(R.drawable.weixin2);
                break;
        }
        transaction.commit();
    }


    private void cleanIcn(){
        weixin.setImageResource(R.drawable.weixin);
        contact.setImageResource(R.drawable.weixin);
        find.setImageResource(R.drawable.weixin);
        me.setImageResource(R.drawable.weixin);
    }
}

对于的activity_main.xml代码

代码语言:txt
AI代码解释
复制
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.dell.myapplication.MainActivity">

    <LinearLayout
        android:id="@+id/ll_button"
        android:gravity="center_vertical"
        android:layout_alignParentBottom="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageButton
            android:layout_weight="1"
            android:id="@+id/weixin1"
            android:background="@null"
            android:src="@drawable/weixin"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <ImageButton
            android:layout_weight="1"
            android:id="@+id/weixin2"
            android:background="@null"
            android:src="@drawable/weixin"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <ImageButton
            android:layout_weight="1"
            android:id="@+id/weixin3"
            android:background="@null"
            android:src="@drawable/weixin"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />
        <ImageButton
            android:layout_weight="1"
            android:id="@+id/weixin4"
            android:background="@null"
            android:src="@drawable/weixin"
            android:layout_width="0dp"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:layout_above="@id/ll_button"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    </LinearLayout>
</RelativeLayout>

接下来是Fragment的代码

下面是fragment的Java代码,每个Java代码都对应这一个布局

代码语言:txt
AI代码解释
复制
package com.example.dell.myapplication;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

public class WeiXinFragment extends Fragment {
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_wei_xin, container, false);
        Button button = (Button) view.findViewById(R.id.btn_weixin);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"微信",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

}
代码语言:txt
AI代码解释
复制
package com.example.dell.myapplication;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;


public class ContactFragment extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_contact, container, false);
        Button button = (Button) view.findViewById(R.id.btn_contact);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"联系人",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

}
代码语言:txt
AI代码解释
复制
package com.example.dell.myapplication;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

/**
 * Created by dell on 2017/8/3.
 */

public class FindFragment extends Fragment {

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_find, container, false);
        Button button = (Button) view.findViewById(R.id.btn_find);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"发现",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }
}
代码语言:txt
AI代码解释
复制
package com.example.dell.myapplication;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;


public class MeFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, container, false);
        Button button = (Button) view.findViewById(R.id.btn_me);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(getActivity(),"我的",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

}

View Code

下面就是Java代码相应的布局代码

fragment_wei_xin.xml

代码语言:txt
AI代码解释
复制
<FrameLayout 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"
    tools:context="com.example.dell.myapplication.WeiXinFragment">

    <Button
        android:id="@+id/btn_weixin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="微信" />

</FrameLayout>

fragment_contact.xml

代码语言:txt
AI代码解释
复制
<FrameLayout 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"
    tools:context="com.example.dell.myapplication.WeiXinFragment">

    <Button
        android:id="@+id/btn_contact"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="联系人" />

</FrameLayout>

fragment_find.xml

代码语言:txt
AI代码解释
复制
<FrameLayout 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"
    tools:context="com.example.dell.myapplication.WeiXinFragment">

    <Button
        android:id="@+id/btn_find"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="发现" />

</FrameLayout>

fragment_me.xml

代码语言:txt
AI代码解释
复制
<FrameLayout 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"
    tools:context="com.example.dell.myapplication.WeiXinFragment">

    <Button
        android:id="@+id/btn_me"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="我的" />

</FrameLayout>

下面是给每个Fragment添加按钮的点击事件,值得注意的是,在Fragment的点击事件跟Activity不一完全相同,在获取空间是不是直接findViewById,

而是要通过获得的view,而获取context不能直接this了,要是用getActivity()

代码语言:txt
AI代码解释
复制
@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_me, container, false);
        //需要通过上面获得的View来findViewById
        Button button = (Button) view.findViewById(R.id.btn_me);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //在这里不能使用this关键字了,要使用getActivity()代替
                Toast.makeText(getActivity(),"我的",Toast.LENGTH_SHORT).show();
            }
        });
        return view;
    }

效果图

项目源代码:https://resource.doiduoyi.com/#231m3ua

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017/08/16 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
安卓开发_慕课网_Fragment实现Tab(App主界面)
学习内容来自“慕课网” 这里用Fragment来实现APP主界面 思路: 底部横向排列4个LinearLayout,每个LinearLayout包含一个图片按钮和一个文字 1、默认显示第一个功能(微信)的图标为亮,其他三个为暗 2、点击相应的按钮,首先将所有的图标变暗,接着隐藏所有Fragment,再把点击的对应的Fragment显示出来,并把相应的图标显示亮 首先布局文件 activity_main.xml与ViewPager实现Tab的是不一样的 1 <LinearLayout xmlns:andro
听着music睡
2018/05/18
1.1K0
底部导航栏的几种实现方式
Android底部导航栏实现方式真的是太多了~在这里仅介绍几种实现方式~建议使用TabLayout +ViewPager ,TabLayout是Android Material Design中的控件,布局文件简单。
小小工匠
2021/08/16
2.5K0
Android Fragment 使用
自从Fragment出现,曾经有段时间,感觉大家谈什么都能跟Fragment谈上关系,做什么都要问下Fragment能实现不~~~哈哈,是不是有点过~~~
全栈程序员站长
2022/07/15
5120
Android Fragment 使用
AndroidStudio制作底部导航栏以及用Fragment实现切换功能
大家好,我是 Vic,今天给大家带来AndroidStudio制作底部导航栏以及用Fragment实现切换功能的概述,希望你们喜欢
达达前端
2019/07/03
7.9K0
工具栏,底部导航栏,可扩展列表视图
这个概念,所接触的很多开发软件或工具,都会碰到。在手机开发的时候,可以进行导航、显示相应的标题等,使开发者不至于在应用程序中迷路。5.0使用Actionbar来实现,很多公司都会对该类进行定制,使用起来更加灵活。5.0之后使用Toolbar来取代之前的Actionbar,这个更加强大。
张哥编程
2024/12/19
2980
使用ViewPager+Fragment实现选项卡切换效果
实现效果 本实例主要实现用ViewPage和Fragment实现选项卡切换效果,选项卡个数为3个,点击选项卡或滑动屏幕会切换Fragment并实现选项卡下方下边框条跟随移动效果。 本程序用androi
古时的风筝
2018/01/08
4K0
使用ViewPager+Fragment实现选项卡切换效果
动态创建Fragment
5.0 在使用fragment的activity里面调用getFragmentManager方法.得到fragmentManager对象
仇诺伊
2018/09/12
2.6K0
Android Fragment 简单实例
  Android上的界面展示都是通过Activity实现的。Activity实在是太经常使用了。我相信大家都已经很熟悉了,这里就不再赘述。 可是Activity也有它的局限性,相同的界面在手机上显示可能很好看,在平板上就未必了,由于平板的屏幕很大。手机的界面放在平板上可能会有过分被拉长、控件间距过大等情况。这个时候更好的体验效果是在Activity中嵌入”小Activity”。然后每个”小Activity”又能够拥有自己的布局。这就是Fragment碎片技术。
全栈程序员站长
2022/07/10
6790
Android Fragment 简单实例
Android Fragment应用实战,使用碎片向ActivityGroup说再见
根据文章内容为技术社区提供优质内容,文章主要讲解了Android开发中的TabLayout实现滑动并点击Tab后滑动失效的问题,以及通过自定义ViewPager2实现滑动不失效的功能。同时,也介绍了一个实用的滑动TabLayout,该TabLayout通过自定义头部和底部布局,实现了类似QQ首页的那种卡片滑动效果。同时,在滑动时通过动态改变布局,实现了布局切换的功能,并且支持手动切换和自动切换,兼容横竖屏切换。
用户1158055
2018/01/05
1.1K0
Android Fragment应用实战,使用碎片向ActivityGroup说再见
Android学习(简单使用Bottom Navigation Activity来实现底部导航栏)
好系统自带的模板往往可以起到事半功倍的效果。下面我们就来看看如何使用Bottom Navigation Activity来完成简单的底部导航栏功能。先来看一下效果图吧:
全栈程序员站长
2022/11/15
2.7K0
Android学习(简单使用Bottom Navigation Activity来实现底部导航栏)
Android Navigation + Fragment 制作APP主页面导航(步骤 + 源码)
  我相信你肯定见过这样的App主页面,底部或者顶部有多个按钮,点击之后会切换当前的页面,滑动当前页面也会切换底部按钮,这里我用几个App的主页面来说明一下吧
晨曦_LLW
2020/12/11
10.6K4
Android Navigation + Fragment 制作APP主页面导航(步骤 + 源码)
Android的Fragment中的互相通信-桥梁activity
Android的Fragment中的互相通信-桥梁activity 效果图如下: image 项目结构图如下: image Fragment1: package com.demo.fragmentto
Dream城堡
2019/03/04
4800
Android Fragment完全解析
我们都知道,Android上的界面展示都是通过Activity实现的,Activity实在是太常用了,我相信大家都已经非常熟悉了,这里就不再赘述。 但是Activity也有它的局限性,同样的界面在手机上显示可能很好看,在平板上就未必了,因为平板的屏幕非常大,手机的界面放在平板上可能会有过分被拉长、控件间距过大等情况。这个时候更好的体验效果是在Activity中嵌入"小Activity",然后每个"小Activity"又可以拥有自己的布局。因此,我们今天的主角Fragment登场了。 Fragment初探 为
欢醉
2018/01/22
8490
Android Fragment完全解析
Android模拟微信主页面的Demo
Android模拟微信主页面的Demo 效果图如下: image 项目结构图如下: image ContanctFragment: package com.demo.moniwexin; impor
Dream城堡
2019/03/04
1K0
Android:多个Fragment切换问题/切换动画设置
在项目开发中,遇到这样一个问题场景:在某个页面(Fragament)中,点击按钮,进行页面部分的切换,即在一个Fragament中嵌套使用了两个Fragament进行切换。
zstar
2022/06/14
6.7K0
Android:多个Fragment切换问题/切换动画设置
相关推荐
安卓开发_慕课网_Fragment实现Tab(App主界面)
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档