Android底部菜單欄(RadioGroup+Fragment)美化
眾所周知,android的底部菜單欄太重要,平時項(xiàng)目一般都是需要用到的,但是網(wǎng)上關(guān)于這方面的demo做得太丑了,實(shí)在慘不忍睹,所以這里便用RadioGroup+Fragment的方式寫了一個,順便美化了一下,需要的可以看下。
效果圖:
項(xiàng)目結(jié)構(gòu)
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(); //添加到數(shù)組 mFragments = new Fragment[]{homeFragment, shopFragment, liveFragment, shoppingCarFragment, mineFragment}; //開啟事務(wù) FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); //添加首頁 ft.add(R.id.fl_content, homeFragment).commit(); //默認(rèn)設(shè)置為第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) { //僅當(dāng)activity為task根(即首個啟動activity)時才生效,這個方法不會改變task中的activity狀態(tài), // 按下返回鍵的作用跟按下HOME效果一樣;重新點(diǎn)擊應(yīng)用還是回到應(yīng)用退出前的狀態(tài); 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的同學(xué)要的話需要自己改一下,時代在進(jìn)步,工具也在升級!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android SDK Manager更新、下載速度慢問題解決辦法
這篇文章主要介紹了Android SDK Manager更新、下載速度慢問題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-05-05Android使用Dialog風(fēng)格彈出框的Activity
這篇文章主要為大家詳細(xì)介紹了Android使用Dialog風(fēng)格彈出框的Activity,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09Android Studio下Flutter環(huán)境搭建圖文教程
這篇文章主要為大家詳細(xì)介紹了Android Studio下Flutter環(huán)境搭建圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-07-07Android 中ScrollView嵌套GridView,ListView的實(shí)例
這篇文章主要介紹了Android 中ScrollView嵌套GridView,ListView的實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-03-03Android編程實(shí)現(xiàn)給Button添加圖片和文字的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)給Button添加圖片和文字的方法,涉及Android針對按鈕元素屬性的相關(guān)操作技巧,需要的朋友可以參考下2015-11-11android自定義彈出框樣式的實(shí)現(xiàn)方法
這篇文章主要為大家詳細(xì)介紹了android自定義彈出框樣式的實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-05-05Android通過滑動實(shí)現(xiàn)Activity跳轉(zhuǎn)(手勢識別器應(yīng)用)
這篇文章主要為大家詳細(xì)介紹了Android通過滑動實(shí)現(xiàn)Activity跳轉(zhuǎn),,講解手勢識別器應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05Android使用自定義控件HorizontalScrollView打造史上最簡單的側(cè)滑菜單
側(cè)滑菜單一般都會自定義ViewGroup,然后隱藏菜單欄,當(dāng)手指滑動時,通過Scroller或者不斷的改變leftMargin等實(shí)現(xiàn);多少都有點(diǎn)復(fù)雜,完成以后還需要對滑動沖突等進(jìn)行處理,今天給大家?guī)硪粋€簡單的實(shí)現(xiàn),史上最簡單有點(diǎn)夸張,但是的確是我目前遇到過的最簡單的一種實(shí)現(xiàn)2016-02-02