底部导航栏是常用的一个工具,大多数的APP都带有底部导航栏,底部导航栏可以方便用户一只手操作,切占用内存比常规的Activity少,底部导航栏使用Fragment+RadioGroup方法来实现,示意图如下:


1.准备8张导航栏的切换图(4张未选中状态,4张选中状态)

ID:radio_msg_0,radio_friend_0,radio_look_0,radio_my_0

radio_msg_1,radio_friend_1,radio_look_1,radio_my_1

2.设置ActivityLayout.xml

(1)放置1个RadioGroup

ID:radiogroup

(2)放置4个RadioButton(根据自己的需要放置RadioButton,一般不超过5个)

ID:btn_0,btn_1,btn_2,btn_3

(3)放置4个Fragment(RadioButton对应的数量)

ID:fragment_msg,fragment_friend,fragment_look,fragment_my

<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"   tools:context="com.example.mytest.MainActivity"> <fragmentandroid:id="@+id/fragment_msg"android:name="com.example.MyFragment.MsgFragment"android:layout_width="match_parent"android:layout_height="80dp"android:layout_above="@+id/radiogroup"android:layout_alignParentTop="true" /><fragmentandroid:id="@+id/fragment_friend"android:name="com.example.MyFragment.FriendFragment"android:layout_width="match_parent"android:layout_height="80dp"android:layout_above="@+id/radiogroup"android:layout_alignParentTop="true" /><fragmentandroid:id="@+id/fragment_look"android:name="com.example.MyFragment.LookFragment"android:layout_width="match_parent"android:layout_height="80dp"android:layout_above="@+id/radiogroup"android:layout_alignParentTop="true" /><fragmentandroid:id="@+id/fragment_my"android:name="com.example.MyFragment.MyFragment"android:layout_width="match_parent"android:layout_height="80dp"android:layout_above="@+id/radiogroup"android:layout_alignParentTop="true" /><RadioGroupandroid:id="@+id/radiogroup"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_alignParentLeft="true"android:layout_alignParentRight="true"android:gravity="center_vertical"android:orientation="horizontal"android:text="female" ><RadioButtonandroid:id="@+id/btn_0"android:layout_width="fill_parent"android:layout_height="54dp"android:layout_alignParentLeft="true"android:layout_weight="1"android:background="@drawable/radio_msg"android:button="@null"android:checked="true"android:gravity="center"android:textColor="@android:color/black"android:textSize="17.0sp" /><RadioButtonandroid:id="@+id/btn_1"android:layout_width="fill_parent"android:layout_height="54dp"android:layout_weight="1"android:background="@drawable/radio_friend"android:button="@null"android:gravity="center"android:textColor="@android:color/black"android:textSize="17.0sp" /><RadioButtonandroid:id="@+id/btn_2"android:layout_width="fill_parent"android:layout_height="54dp"android:layout_weight="1"android:background="@drawable/radio_look"android:button="@null"android:gravity="center"android:textColor="@android:color/black"android:textSize="17.0sp" /><RadioButtonandroid:id="@+id/btn_3"android:layout_width="fill_parent"android:layout_height="54dp"android:layout_weight="1"android:background="@drawable/radio_my"android:button="@null"android:gravity="center"android:textColor="@android:color/black"android:textSize="17.0sp" /></RadioGroup></RelativeLayout>

3.设置每个Radio的xml布局

XML ID:radio_msg,radio_friend,radio_look,radio_my

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">   <item   android:state_checked="false"   android:drawable="@drawable/radio_msg_0" />   <item   android:state_checked="true"   android:drawable="@drawable/radio_msg_1" />
</selector>  
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">   <item   android:state_checked="false"   android:drawable="@drawable/radio_friend_0" />   <item   android:state_checked="true"   android:drawable="@drawable/radio_friend_1" />
</selector>  
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">   <item   android:state_checked="false"   android:drawable="@drawable/radio_look_0" />   <item   android:state_checked="true"   android:drawable="@drawable/radio_look_1" />
</selector>  
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">   <item   android:state_checked="false"   android:drawable="@drawable/radio_my_0" />   <item   android:state_checked="true"   android:drawable="@drawable/radio_my_1" />
</selector>  

4.设置每个Fragment的xml布局

XML ID:fragment_msg,fragment_friend,fragment_friend,fragment_friend

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/text2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="这是聊天" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/text1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="这是好友" /></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/text4"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="这是看点" /></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"><TextViewandroid:id="@+id/text3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerInParent="true"android:text="这是我的" /></RelativeLayout>

5.建立继承Fragment类 新建一个package用来存放4个Fragment类

ID:MsgFragment,FriendFragment,LookFragment,MyFragment

package com.example.MyFragment;import com.example.mytest.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;public class MsgFragment extends Fragment
{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {//mxl文件return inflater.inflate(R.drawable.fragment_msg, container, false);}}
package com.example.MyFragment;import com.example.mytest.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;public class FriendFragment extends Fragment
{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {//mxl文件return inflater.inflate(R.drawable.fragment_friend, container, false);}}
package com.example.MyFragment;import com.example.mytest.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;public class LookFragment extends Fragment
{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {//mxl文件return inflater.inflate(R.drawable.fragment_look, container, false);}}
package com.example.MyFragment;import com.example.mytest.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;public class MyFragment extends Fragment
{@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {//mxl文件return inflater.inflate(R.drawable.fragment_my, container, false);}}

6.在Activity里设置Activity

package com.example.mytest;import com.example.MyFragment.FriendFragment;
import com.example.MyFragment.MsgFragment;
import com.example.MyFragment.MyFragment;
import com.example.mytest.R;import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.RadioButton;
import android.widget.RadioGroup;public class MainActivity extends Activity
{private MsgFragment    fragment1;private FriendFragment fragment2;private MyFragment     fragment3;private RadioGroup  radioGroup;private RadioButton radioButton;//private FragmentManager manager;//private FragmentTransaction transaction;@Overrideprotected void onCreate(Bundle savedInstanceState) {//全屏显示,显示时间和电量requestWindowFeature(Window.FEATURE_NO_TITLE);getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//开启事务管理,主要处理Fragment//manager = getFragmentManager();// transaction = manager.beginTransaction();//设置切换FragmentradioGroup = (RadioGroup)findViewById(R.id.radiogroup);RadioGroupList radigGroupList = new RadioGroupList();radioGroup.setOnCheckedChangeListener(radigGroupList);//设置默认按钮为选中状态radioButton =(RadioButton) findViewById(R.id.btn_0);radioButton.setChecked(true);//开始处理Fragmentfragment1 = new MsgFragment();fragment2 = new FriendFragment();fragment3 = new MyFragment();findViewById(R.id.fragment_msg).setVisibility(View.INVISIBLE);findViewById(R.id.fragment_friend).setVisibility(View.INVISIBLE);findViewById(R.id.fragment_look).setVisibility(View.INVISIBLE);findViewById(R.id.fragment_my).setVisibility(View.INVISIBLE);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}public class RadioGroupList implements RadioGroup.OnCheckedChangeListener{@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if(group.getId() == R.id.radiogroup){switch (checkedId){case R.id.btn_0:findViewById(R.id.fragment_msg).setVisibility(View.VISIBLE);findViewById(R.id.fragment_friend).setVisibility(View.INVISIBLE);findViewById(R.id.fragment_look).setVisibility(View.INVISIBLE);findViewById(R.id.fragment_my).setVisibility(View.INVISIBLE);Log.d("消息", "提示");break;case R.id.btn_1:findViewById(R.id.fragment_msg).setVisibility(View.INVISIBLE);findViewById(R.id.fragment_friend).setVisibility(View.VISIBLE );findViewById(R.id.fragment_look).setVisibility(View.INVISIBLE);findViewById(R.id.fragment_my).setVisibility(View.INVISIBLE);Log.d("好友", "提示");break;case R.id.btn_2:findViewById(R.id.fragment_msg).setVisibility(View.INVISIBLE);findViewById(R.id.fragment_friend).setVisibility(View.INVISIBLE);findViewById(R.id.fragment_look).setVisibility(View.VISIBLE);findViewById(R.id.fragment_my).setVisibility(View.INVISIBLE );Log.d("看点", "提示");break;case R.id.btn_3:findViewById(R.id.fragment_msg).setVisibility(View.INVISIBLE);findViewById(R.id.fragment_friend).setVisibility(View.INVISIBLE);findViewById(R.id.fragment_look).setVisibility(View.INVISIBLE);findViewById(R.id.fragment_my).setVisibility(View.VISIBLE );Log.d("我的", "提示");break;default :break;}}}    }
}

这就是最简单的底部导航栏实现方法,原理非常简单,只要学过一周Android开发的朋友都可以使用,但是这个程序有一个特点是不能实现滑动,ViewPage的实现方法,可以到我的博客看下。

Demo下载地址:https://download.csdn.net/download/nonecode/10625735

Android 底部导航栏-极致简单版相关推荐

  1. Android 底部导航栏的简单实现-BottomNavigationView

    在谷歌官方发布BottomNavigationView控件之前我们可以自己组合控件实现,比如LinearLayout + TextView(使用android:drawableTop属性+select ...

  2. android fragment 底部菜单栏,一句话搞定Android底部导航栏,一键绑定Fragment、ViewPager...

    现在大多数App都会用到底部导航栏,比如常见的聊天工具QQ.微信.购物App等等,有了底部导航栏,用户可以随时切换界面,查看不同的内容.它的实现方式也很多,以前大多使用TabHost来实现,但是现在我 ...

  3. 转载:Android底部导航栏,三种风格和实现

    原文出处 标题:Android底部导航栏,三种风格和实现 作者:阿飞__ 原文链接:Android底部导航栏,三种风格和实现_阿飞__的博客-CSDN博客_android导航栏 一.效果图展示 如果动 ...

  4. Android底部导航栏的三种风格实现

    一.效果图展示 如果动图没有动的话,也可以看下面这个静态图 以下挨个分析每个的实现,这里只做简单的效果展示,大家可以基于目前代码做二次开发. 二.BottomNavigationView 这是 Goo ...

  5. Android底部导航栏最常用的两种写法

    先来看看底部导航栏的效果 Android 底部导航栏有很多种写法,例如: RadioGroup , Tablayout, TabHost  , LinearLayout + ImageView + T ...

  6. Android底部导航栏+消息提醒

    Android底部导航栏+消息提醒 最近想在网上找一些Android底部导航栏切换并能提供消息提醒的案例,虽然有很多案例但都不是我想要的.我就开始自己瞎研究了,废话不多说了,直接上代码. 1.先创建一 ...

  7. android 固定底部导航,如何设置android底部导航栏位置固定在android

    请帮我设置底部导航栏位置固定在底部, ,因为我在输入editText字段时遇到问题,底部导航栏向上移动并覆盖其他领域如何设置android底部导航栏位置固定在android 代码: xmlns:and ...

  8. Android底部导航栏切换页面填坑

    ** Android底部导航栏切换页面填坑 ** 这个效果的实现关键点就是给选项赋予两种状态,focused和normal,在主程序中用监听判断是否被选中,就给被选中的选项设focused为true, ...

  9. Android底部导航栏(带加号、红点提示、数字消息)

    前言 因为公司好多项目会用到底部导航栏,大都千篇一律,无非2-5个Tab(可能会有些点击动画.红点提示或者中间多个加号)总是重复相同的操作...所以...很懒的我希望几行代码就能实现这个效果(少敲一行 ...

最新文章

  1. 为什么现有的工作制度 对 上班族不利
  2. Windows窗体编程(二)
  3. RSS - 简单方便的follow资讯
  4. matlab曲线拟合法,MATLAB曲线拟合
  5. codis 部署和测试
  6. 支付宝支付回调是什么意思_支付宝邮箱是什么
  7. logstash-input-redis插件使用详解
  8. url中隐藏php后缀,url中如何隐藏.php
  9. 使用CompletionService结合ExecutorService批处理任务
  10. 华为荣耀盒子显示服务器忙,华为荣耀盒子m321连接后死机了怎么办?教你三大解决方法...
  11. linux HUSTOJ 一些页面修改
  12. hp-unix 自带磁带机备份数据
  13. 娱乐而已,认真你就输了
  14. 理解对比表示学习(Contrastive Learning)
  15. 2021年真无线蓝牙耳机排名:人气排名前十的蓝牙耳机推荐
  16. 解决:Jackson反序列化Java内部类失败(序列化后的识别码为LinkedHashMap,而非内部类本身)
  17. 09-Scrum过程-评审会(Review Meeting) 反思会(Retrospective Meeting)
  18. 牛客-紫魔法师(仙人掌染色-判奇环)
  19. Sers微服务2.1.1
  20. 国家高新技术企业认定管理工作网

热门文章

  1. Frog青蛙的约会【浙江省选2002】(数论)
  2. cf 1324D. Pair of Topics
  3. 人民币大小写金额转换
  4. QT简单入门程序——实现可修改用户信息界面
  5. 店宝宝:巨头PK精品电商
  6. 【译】Inside SafetyNet - part 3
  7. Java 17的这些新特性,Java迈入新时代
  8. 网站浏览器崩溃原因分析
  9. NMOS的栅极充电过程
  10. Linux 性能监控分析