Android使用RadioGroup实现底部导航栏
程序员文章站
2024-02-11 08:10:40
radiogroup实现底部导航栏效果,如图::
实现可最基本的导航栏功能,不能左右滑动,只能点击
1.内嵌的fragment的布局:...
radiogroup实现底部导航栏效果,如图::
实现可最基本的导航栏功能,不能左右滑动,只能点击
1.内嵌的fragment的布局:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <textview android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:textsize="50sp" android:textcolor="@color/colorprimary" android:text="home"/> </linearlayout>
2.fragment的activity代码:
public class frhome extends fragment { @nullable @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view view = layoutinflater.from(getcontext()).inflate(r.layout.fragment_home, container, false); return view; } }
以此为例根据需要编写不同的fragment布局等等。
3.装载fragment的界面布局如下(其中使用了selector进行实现点击改变图标和文字颜色):
点击改变文字颜色:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:color="#3f51b5"/> <item android:state_checked="false" android:color="#8f8f8f"/> </selector>
点击改变图标:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_checked="true" android:drawable="@mipmap/ic_history_checked"/> <item android:state_checked="false" android:drawable="@mipmap/ic_history_unchecked"/> </selector>
界面布局:
<?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" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context="com.lotus.chartspagedemo.acthome"> <framelayout android:id="@+id/frame_layout" android:layout_width="match_parent" android:layout_above="@+id/card_view" android:layout_height="match_parent"/> <android.support.v7.widget.cardview android:id="@+id/card_view" app:cardelevation="25dp" android:layout_alignparentbottom="true" android:layout_width="match_parent" android:layout_height="wrap_content"> <radiogroup android:paddingtop="5dp" android:id="@+id/tab_bar" android:background="@color/app_white" android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center" android:orientation="horizontal"> <radiobutton android:id="@+id/tab_home" android:gravity="center" android:button="@null" android:drawabletop="@drawable/selector_tab_home" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textcolor="@drawable/selector_tab_color" android:text="首页"/> <radiobutton android:id="@+id/tab_health" android:gravity="center" android:button="@null" android:drawabletop="@drawable/selector_tab_health" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textcolor="@drawable/selector_tab_color" android:text="体检测评" /> <radiobutton android:id="@+id/tab_personal" android:gravity="center" android:button="@null" android:drawabletop="@drawable/selector_tab_personal" android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" android:textcolor="@drawable/selector_tab_color" android:text="个人中心" /> </radiogroup> </android.support.v7.widget.cardview> </relativelayout>
4.装载fragment的界面的activity代码(加入双击返回键则退出应用):
public class acthome extends fragmentactivity implements radiogroup.oncheckedchangelistener { @bindview(r.id.frame_layout) framelayout framelayout; @bindview(r.id.tab_home) radiobutton tabhome; @bindview(r.id.tab_health) radiobutton tabhealth; @bindview(r.id.tab_personal) radiobutton tabpersonal; @bindview(r.id.tab_bar) radiogroup tabbar; public final static string action_exit_system = "sys_exit"; private fragmentmanager manager; private fragmenttransaction transaction; private frhome frhome; private frhealth frhealth; private frpersonal frpersonal; private long mexittime; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_home); butterknife.bind(this); radiobutton tabhome = (radiobutton) tabbar.getchildat(0); tabhome.setchecked(true); tabbar.setoncheckedchangelistener(this); initfragment(); } private void initfragment() { manager = getsupportfragmentmanager(); transaction = manager.begintransaction(); frhome = new frhome(); transaction.add(r.id.frame_layout,frhome); transaction.commit(); } @override public void oncheckedchanged(radiogroup radiogroup, @idres int checkedid) { switch (checkedid) { case r.id.tab_home: fragmenttransaction ft1 = manager.begintransaction(); hideall(ft1); if (frhome!=null){ ft1.show(frhome); }else { frhome=new frhome(); ft1.add(r.id.frame_layout,frhome); } ft1.commit(); break; case r.id.tab_health: fragmenttransaction ft2 = manager.begintransaction(); hideall(ft2); if (frhealth!=null){ ft2.show(frhealth); }else { frhealth = new frhealth(); ft2.add(r.id.frame_layout,frhealth); } ft2.commit(); break; case r.id.tab_personal: fragmenttransaction ft5 = manager.begintransaction(); hideall(ft5); if (frpersonal!=null){ ft5.show(frpersonal); }else { frpersonal = new frpersonal(); ft5.add(r.id.frame_layout, frpersonal); } ft5.commit(); break; } } private void hideall(fragmenttransaction ft){ if (ft==null){ return; } if (frhome!=null){ ft.hide(frhome); } if (frhealth!=null){ ft.hide(frhealth); } if (frpersonal!=null){ ft.hide(frpersonal); } } @override public void onbackpressed() { if ((system.currenttimemillis() - mexittime) > 2000) { toast.maketext(acthome.this,"再按一次退出程序",toast.length_short).show(); mexittime = system.currenttimemillis(); } else { new handler().postdelayed(new runnable() { @override public void run() { onexit(acthome.this); } }, 500); } } public static void onexit(final context context) { try { intent intent = new intent(); intent.setaction(context.getapplicationcontext().getpackagename() + action_exit_system); context.sendbroadcast(intent); // mobclickagent.onkillprocess(context); new handler().postdelayed(new runnable() { @override public void run() { system.exit(0); } }, 200); } catch (exception e) { e.printstacktrace(); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读
-
Android使用RadioGroup实现底部导航栏
-
Android首页底部导航栏实现方式
-
Android实现底部状态栏切换的两种方式
-
flutter BottomAppBar实现不规则底部导航栏
-
Android底部菜单栏(RadioGroup+Fragment)美化
-
Android实现底部导航栏功能(选项卡)
-
Android 使用AlarmManager和NotificationManager来实现闹钟和通知栏
-
Android自定义ViewPagerIndicator实现炫酷导航栏指示器(ViewPager+Fragment)
-
Android中TabLayout+ViewPager 简单实现app底部Tab导航栏
-
Android不使用自定义布局情况下实现自定义通知栏图标的方法