安卓底部导航栏BottomNavigationView

2023-11-28 16:36

本文主要是介绍安卓底部导航栏BottomNavigationView,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

1. BottomNavigationView

(1) 准备BottomNavigationView使用的菜单资源文件

(2) 准备颜色选择器

(3) BottomNavigationView控件设置

 (4) 在Java代码中设置OnItemSelectedListener监听器

(5) 与Fragment配合

2. BottomTabBar


实现安卓底部导航栏,google为我们提供了BottomNavigationView类。第三方提供了简易版BottomTabBar类

1. BottomNavigationView

(1) 准备BottomNavigationView使用的菜单资源文件

res / menu / xxx.xml

使用BottonNavigationView中 app:menu属性 设置。

//例.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"tools:showIn="navigation_view"><item android:id="@+id/item1"android:icon="@drawable/ic_home_main"android:title="home"/><item android:id="@+id/item2"android:icon="@drawable/ic_home_butt"android:title="butt"/><item android:id="@+id/item3"android:icon="@drawable/ic_home_chat"android:title="chat"/><item android:id="@+id/item4"android:icon="@drawable/ic_home_mime"android:title="mime"/>
</menu>

(2) 准备颜色选择器

BottomNavigationView的颜色通常设置为颜色选择器,然后使用 app:itemTextColor属性 设置文字颜色,使用 app:itemIconTint属性 设置图标颜色,也可不进行设置。

res / color / xxx.xml

//例.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"><item android:color="@color/purple_700" android:state_checked="true"/><item android:color="@color/black" android:state_checked="false"/>
</selector>

(3) BottomNavigationView控件设置

<com.google.android.material.bottomnavigation.BottomNavigationViewandroid:id="@+id/bottomNavigationView"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"app:menu="@menu/bottom_navigation_view_menu"app:itemTextColor="@color/my_color"app:itemIconTint="@color/my_color" />

 (4) 在Java代码中设置OnItemSelectedListener监听器

BottomNavigationView bottomNavigationView=findViewById(R.id.bottomNavigationView);
bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {public boolean onNavigationItemSelected(MenuItem item) {// 选择itemif( item.getItemId() == R.id. itemX ){//判断点击的Item的Id是否是指定Item的Id}//必须返回true,返回falseBottomNavigationView将不变化return true;}
});

(5) 与Fragment配合

BottomNavigationView与Fragment的配合一般通过onItemSelectedListener监听器

获取碎片管理者,需使用getSupportFragmentManager(),而getFragmentManager()已淘汰

public class MainActivity extends AppCompatActivity {private Fragment f1,f2,f3,f4;private FragmentManager fragmentManager;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);f1=new Fragment1();f2=new Fragment2();f3=new Fragment3();f4=new Fragment4();//获取碎片管理者,需使用getSupportFragmentManager(),而getFragmentManager()已淘汰fragmentManager=getSupportFragmentManager();FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();fragmentTransaction.add(R.id.frameLayout,f1);fragmentTransaction.add(R.id.frameLayout,f2);fragmentTransaction.add(R.id.frameLayout,f3);fragmentTransaction.add(R.id.frameLayout,f4);fragmentTransaction.hide(f2);fragmentTransaction.hide(f3);fragmentTransaction.hide(f4);fragmentTransaction.commit();BottomNavigationView bottomNavigationView=findViewById(R.id.bottomNavigationView);bottomNavigationView.setOnItemSelectedListener(new NavigationBarView.OnItemSelectedListener() {private int nowFragment=R.id.item1;public boolean onNavigationItemSelected(@NonNull MenuItem item) {int itemId=item.getItemId();FragmentTransaction fragmentTransaction= fragmentManager.beginTransaction();switch (nowFragment){case R.id.item1:fragmentTransaction.hide(f1);break;case R.id.item2:fragmentTransaction.hide(f2);break;case R.id.item3:fragmentTransaction.hide(f3);break;case R.id.item4:fragmentTransaction.hide(f4);break;}switch (itemId){case R.id.item1:fragmentTransaction.show(f1);break;case R.id.item2:fragmentTransaction.show(f2);break;case R.id.item3:fragmentTransaction.show(f3);break;case R.id.item4:fragmentTransaction.show(f4);break;}fragmentTransaction.commit();nowFragment=itemId;return true;}});}
}

2. BottomTabBar

该方法于2018.8.25进行最后一次更新,不建议使用。

compile 'com.hjm:BottomTabBar:1.2.2'
    <com.hjm.bottomtabbar.BottomTabBarandroid:id="@+id/bottom_tab_bar"android:layout_width="match_parent"android:layout_height="match_parent"  hjm:tab_bar_background="#FFFFFF"                     //BottomTabBar的整体背景颜色hjm:tab_divider_background="#FF0000"                 //分割线背景hjm:tab_font_size="14px"                             //文字尺寸hjm:tab_img_font_padding="0"                       //图片文字间隔hjm:tab_img_height="70px"                            //图片高度hjm:tab_img_width="70px"                            //图片宽度hjm:tab_isshow_divider="true"                        //是否显示分割线hjm:tab_padding_bottom="5px"                        //下边距hjm:tab_padding_top="5px"                           //上边距hjm:tab_selected_color="#000000"                     //选中的颜色hjm:tab_unselected_color="@color/colorPrimary"/>     //未选中的颜色
 mBottomBar = findViewById(R.id.bottom_bar);mBottomBar.init(getSupportFragmentManager(), 720, 1280)
//                .setImgSize(70, 70)
//                .setFontSize(14)
//                .setTabPadding(5, 0, 5)
//                .setChangeColor(Color.parseColor("#FF00F0"),Color.parseColor("#CCCCCC")).addTabItem("第一项", R.mipmap.home_selected, R.mipmap.home, OneFragment.class).addTabItem("第二项", R.mipmap.list, TwoFragment.class).addTabItem("第三项", R.mipmap.user, ThreeFragment.class)
//                .isShowDivider(true)
//                .setDividerColor(Color.parseColor("#FF0000"))
//                .setTabBarBackgroundColor(Color.parseColor("#00FF0000")).setOnTabChangeListener(new BottomTabBar.OnTabChangeListener() {@Overridepublic void onTabChange(int position, String name, View view) {if (position == 1)mBottomBar.setSpot(1, false);}}).setSpot(1, true).setSpot(2, true);

详见:超简单,几行代码搞定Android底部导航栏 - 简书 (jianshu.com)

这篇关于安卓底部导航栏BottomNavigationView的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/429970

相关文章

安卓链接正常显示,ios#符被转义%23导致链接访问404

原因分析: url中含有特殊字符 中文未编码 都有可能导致URL转换失败,所以需要对url编码处理  如下: guard let allowUrl = webUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {return} 后面发现当url中有#号时,会被误伤转义为%23,导致链接无法访问

消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法

消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法   消除安卓SDK更新时的“https://dl-ssl.google.com refused”异常的方法 [转载]原地址:http://blog.csdn.net/x605940745/article/details/17911115 消除SDK更新时的“

安卓玩机工具------小米工具箱扩展工具 小米机型功能拓展

小米工具箱扩展版                     小米工具箱扩展版 iO_Box_Mi_Ext是由@晨钟酱开发的一款适用于小米(MIUI)、多亲(2、2Pro)、多看(多看电纸书)的多功能工具箱。该工具所有功能均可以免root实现,使用前,请打开开发者选项中的“USB调试”  功能特点 【小米工具箱】 1:冻结MIUI全家桶,隐藏状态栏图标,修改下拉通知栏图块数量;冻结

安卓开发板_联发科MTK开发评估套件串口调试

串口调试 如果正在进行lk(little kernel ) 或内核开发,USB 串口适配器( USB 转串口 TTL 适配器的简称)对于检查系统启动日志非常有用,特别是在没有图形桌面显示的情况下。 1.选购适配器 常用的许多 USB 转串口的适配器,按芯片来分,有以下几种: CH340PL2303CP2104FT232 一般来说,采用 CH340 芯片的适配器,性能比较稳定,价

安卓实现弹出软键盘屏幕自适应调整

今天,我通过尝试诸多方法,最终实现了软键盘弹出屏幕的自适应。      其实,一开始我想通过EditText的事件来实现,后来发现,安卓自带的函数十分强大,只需几行代码,便可实现。实现如下:     在Manifest中设置activity的属性:android:windowSoftInputMode="adjustUnspecified|stateHidden|adjustResi

安卓错误经验分析之 R cannot be resolved to a variable

当出现 R cannot be resolved to a variable  错误的时候,不能采用编译器建议的修改方法,试着clean一下,然后查找gen文件夹下R.java是否丢失,如果不存在R.java,程序没有报错且采用其它方法均无效,八成是res文件夹下的layout或者manifest出现错误没有显示出来,需要自己查一遍,否则无法根本解决问题,盲目修改代码是没用的。

CSS学习6--背景图片、颜色、位置、附着、简写、透明、缩放、多背景、凹凸文字、导航栏例子

CSS背景 一、背景颜色和图片二、背景位置三、背景附着四、背景简写五、背景透明六、背景缩放七、多背景八、凹凸文字九、导航栏例子 一、背景颜色和图片 background-color: pink; 背景颜色backgroundoimage: url(##.jpg); 背景图片background-repeat: 平铺 repeat-x横向平铺,repeat-y纵向平铺; 平铺不到

uni-app 扫码优化:谈谈我是如何提升安卓 App 扫码准确率的

一. 前言 之前的一个项目遭到用户吐槽:“你们这个 App 扫码的正确率太低了,尤其是安卓的设备。经常性的扫码扫不出来,就算是扫出来了,也是错误的结果!” 由于之前是扫描二维码的需求,所以没有对扫描条形码做严格的测试,客户提示说是条形码扫描效率低下。随即,我用自己的手机测试了一下,在安卓手机上确实有这样的问题,扫码准确率确实是低,尤其是条形码,扫码效率慢且不准确。扫描二维码的的效率还算可以

安卓金币字符串转换成三位一个逗号的格式

DecimalFormat df1 = new DecimalFormat("###,###,###,##0.##");         return df1.format(Double.parseDouble(“ 17352208.25 ”)); 输出   17,352,208,25

安卓开发的无线adb

无线adb调试程序,和 电脑手机之间传输文件 使用无线 adb 从 远程手机 上 下载 文件 adb pull sdcard/mapgis/data G: 将 data 文件夹里的 所有 文件 下载 到 电脑 的 G: 盘目录下 现在我有一个小方法说不定可以帮助某些人哦(多为不是android开发人员) 1. 条件如下: android 手机  wifi无线连