Android底部菜单栏(RadioGroup+Fragment)美化
程序员文章站
2022-04-09 16:43:51
众所周知,android的底部菜单栏太重要,平时项目一般都是需要用到的,但是网上关于这方面的demo做得太丑了,实在惨不忍睹,所以这里便用radiogroup+fragment的方式写了一个,顺便美化...
众所周知,android的底部菜单栏太重要,平时项目一般都是需要用到的,但是网上关于这方面的demo做得太丑了,实在惨不忍睹,所以这里便用radiogroup+fragment的方式写了一个,顺便美化了一下,需要的可以看下。
效果图:
项目结构
mainactivity.java
public class mainactivity extends appcompatactivity { private framelayout framelayout; private radiogroup radiogroup; private fragment[] mfragments; private int mindex; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initfragment(); setradiogrouplistener(); } private void initfragment() { radiogroup = (radiogroup) findviewbyid(r.id.radiogroup); framelayout = (framelayout) findviewbyid(r.id.fl_content); homefragment homefragment = new homefragment(); shopfragment shopfragment = new shopfragment(); livefragment livefragment = new livefragment(); shoppingcarfragment shoppingcarfragment = new shoppingcarfragment(); minefragment minefragment = new minefragment(); //添加到数组 mfragments = new fragment[]{homefragment, shopfragment, livefragment, shoppingcarfragment, minefragment}; //开启事务 fragmenttransaction ft = getsupportfragmentmanager().begintransaction(); //添加首页 ft.add(r.id.fl_content, homefragment).commit(); //默认设置为第0个 setindexselected(0); } private void setindexselected(int index) { if (mindex == index) { return; } fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmenttransaction ft = fragmentmanager.begintransaction(); //隐藏 ft.hide(mfragments[mindex]); //判断是否添加 if (!mfragments[index].isadded()) { ft.add(r.id.fl_content, mfragments[index]).show(mfragments[index]); } else { ft.show(mfragments[index]); } ft.commit(); //再次赋值 mindex = index; } private void setradiogrouplistener() { radiogroup.setoncheckedchangelistener(new radiogroup.oncheckedchangelistener() { @override public void oncheckedchanged(radiogroup radiogroup, int i) { switch (i) { case r.id.rb_home: setindexselected(0); break; case r.id.rb_shop: setindexselected(1); break; case r.id.rb_live: setindexselected(2); break; case r.id.rb_shopping_car: setindexselected(3); break; case r.id.rb_mine: setindexselected(4); break; default: setindexselected(0); break; } } }); } @override public boolean onkeydown(int keycode, keyevent event) { if (keycode == keyevent.keycode_back) { //仅当activity为task根(即首个启动activity)时才生效,这个方法不会改变task中的activity状态, // 按下返回键的作用跟按下home效果一样;重新点击应用还是回到应用退出前的状态; movetasktoback(false); return true; } return super.onkeydown(keycode, event); } }
fragment,这里只列出homefragment的,其他都是一样
public class homefragment extends basefragment { public homefragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view=inflater.inflate(r.layout.fragment_home,container,false); return view; } }
activity_main.xml布局文件
<?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:layout_width="match_parent" android:layout_height="match_parent"> <framelayout android:id="@+id/fl_content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/line" /> <view android:id="@+id/line" android:layout_width="match_parent" android:layout_height="@dimen/line_size" android:layout_above="@+id/radiogroup" android:background="#9e9e9e" /> <radiogroup android:id="@+id/radiogroup" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:clickable="true" android:gravity="center" android:orientation="horizontal" android:padding="3dp"> <radiobutton android:id="@+id/rb_home" style="@style/radiobuttonstyle" android:checked="true" android:drawabletop="@drawable/btn_home" android:text="@string/home" /> <radiobutton android:id="@+id/rb_shop" style="@style/radiobuttonstyle" android:drawabletop="@drawable/btn_shop" android:text="@string/shop" /> <radiobutton android:id="@+id/rb_live" style="@style/radiobuttonstyle" android:drawabletop="@drawable/btn_live" android:text="@string/live" /> <radiobutton android:id="@+id/rb_shopping_car" style="@style/radiobuttonstyle" android:drawabletop="@drawable/btn_shopping_car" android:text="@string/shopping_car" /> <radiobutton android:id="@+id/rb_mine" style="@style/radiobuttonstyle" android:drawabletop="@drawable/btn_mine" android:text="@string/mine" /> </radiogroup> </relativelayout>
radiobutton的样式
<style name="radiobuttonstyle"> <item name="android:layout_width">0dp</item> <item name="android:layout_weight">1</item> <item name="android:layout_height">match_parent</item> <item name="android:layout_marginright">10dp</item> <item name="android:layout_marginleft">10dp</item> <item name="android:button">@null</item> <item name="android:gravity">center</item> <item name="android:textcolor">@color/color_radiobutton</item> <item name="android:textsize">10sp</item> </style>
demo下载地址:底部菜单栏
温馨提示:以后我自己写的demo都是用android studio写的了,用eclipse的同学要的话需要自己改一下,时代在进步,工具也在升级!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: Vue 监听元素前后变化值实例
下一篇: Jmeter参数化获取序列数据实现过程