Android使用RadioGroup實(shí)現(xiàn)底部導(dǎo)航欄
RadioGroup實(shí)現(xiàn)底部導(dǎo)航欄效果,如圖::

實(shí)現(xiàn)可最基本的導(dǎo)航欄功能,不能左右滑動(dòng),只能點(diǎn)擊
1.內(nèi)嵌的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;
}
}
以此為例根據(jù)需要編寫不同的fragment布局等等。
3.裝載fragment的界面布局如下(其中使用了selector進(jìn)行實(shí)現(xiàn)點(diǎn)擊改變圖標(biāo)和文字顏色):
點(diǎn)擊改變文字顏色:
<?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>
點(diǎn)擊改變圖標(biāo):
<?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="體檢測(cè)評(píng)" /> <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="個(gè)人中心" /> </RadioGroup> </android.support.v7.widget.CardView> </RelativeLayout>
4.裝載fragment的界面的activity代碼(加入雙擊返回鍵則退出應(yīng)用):
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();
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 彈出Dialog時(shí)隱藏狀態(tài)欄和底部導(dǎo)航欄的方法
- 解決android 顯示內(nèi)容被底部導(dǎo)航欄遮擋的問題
- android 全屏去掉底部虛擬導(dǎo)航欄的方法
- 超簡(jiǎn)單的幾行代碼搞定Android底部導(dǎo)航欄功能
- Android用Scroller實(shí)現(xiàn)一個(gè)可向上滑動(dòng)的底部導(dǎo)航欄
- Android程序開發(fā)之Fragment實(shí)現(xiàn)底部導(dǎo)航欄實(shí)例代碼
- Android 中使用RadioGroup和Fragment實(shí)現(xiàn)底部導(dǎo)航欄的功能
相關(guān)文章
Android?Flutter繪制有趣的?loading加載動(dòng)畫
在網(wǎng)絡(luò)速度較慢的場(chǎng)景,一個(gè)有趣的加載會(huì)提高用戶的耐心和對(duì)?App?的好感。本篇我們利用Flutter?的?PathMetric來玩幾個(gè)有趣的?loading?效果,感興趣的可以動(dòng)手嘗試一下2022-07-07
Android設(shè)備adb連接后顯示device unauthorized解決方案
這篇文章主要為大家介紹了Android設(shè)備adb連接后顯示device unauthorized解決方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Android fragment實(shí)現(xiàn)按鈕點(diǎn)擊事件的示例講解
下面小編就為大家分享一篇Android fragment實(shí)現(xiàn)按鈕點(diǎn)擊事件的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android使用自定義View實(shí)現(xiàn)橫行時(shí)間軸效果
這篇文章主要給大家介紹了關(guān)于Android使用自定義View實(shí)現(xiàn)橫行時(shí)間軸效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Android具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-12-12
Android編程基于自定義控件實(shí)現(xiàn)時(shí)鐘功能的方法
這篇文章主要介紹了Android編程基于自定義控件實(shí)現(xiàn)時(shí)鐘功能的方法,結(jié)合實(shí)例形式詳細(xì)分析了Android自定義控件的定義及時(shí)鐘功能相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-03-03
Android ActionBarActivity設(shè)置全屏無標(biāo)題實(shí)現(xiàn)方法總結(jié)
這篇文章主要介紹了Android ActionBarActivity設(shè)置全屏無標(biāo)題實(shí)現(xiàn)方法總結(jié)的相關(guān)資料,需要的朋友可以參考下2017-04-04
Android Studio升級(jí)3.6 Build窗口出現(xiàn)中文亂碼問題解決方法
這篇文章主要介紹了Android Studio升級(jí)3.6 Build窗口出現(xiàn)中文亂碼問題解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Android實(shí)現(xiàn)APP歡迎頁面簡(jiǎn)單制作思路
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)APP歡迎頁面簡(jiǎn)單制作思路,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android如何調(diào)用系統(tǒng)相機(jī)拍照
這篇文章主要為大家詳細(xì)介紹了Android如何調(diào)用系統(tǒng)相機(jī)拍照的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09

