亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

Android底部菜單欄(RadioGroup+Fragment)美化

瀏覽:7日期:2022-09-23 11:18:15

眾所周知,android的底部菜單欄太重要,平時項(xiàng)目一般都是需要用到的,但是網(wǎng)上關(guān)于這方面的demo做得太丑了,實(shí)在慘不忍睹,所以這里便用RadioGroup+Fragment的方式寫了一個,順便美化了一下,需要的可以看下。

效果圖:

Android底部菜單欄(RadioGroup+Fragment)美化

項(xiàng)目結(jié)構(gòu)

Android底部菜單欄(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(); //添加到數(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: android:layout_width='match_parent' android:layout_height='match_parent' android:layout_above='@+id/line' /> <View android: android:layout_width='match_parent' android:layout_height='@dimen/line_size' android:layout_above='@+id/radioGroup' android:background='#9e9e9e' /> <RadioGroup android: 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:android:checked='true' android:drawableTop='@drawable/btn_home' android:text='@string/home' /> <RadioButton android:android:drawableTop='@drawable/btn_shop' android:text='@string/shop' /> <RadioButton android:android:drawableTop='@drawable/btn_live' android:text='@string/live' /> <RadioButton android:android:drawableTop='@drawable/btn_shopping_car' android:text='@string/shopping_car' /> <RadioButton android: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í)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Android
相關(guān)文章:
主站蜘蛛池模板: 久久九九综合 | 亚洲综合日韩精品欧美综合区 | 国产在线麻豆精品观看 | 国产一区中文字幕 | 国产真实伦在线观看 | 国产成人免费观看在线视频 | 日韩黄色小视频 | 亚洲激情成人网 | 欧美黄色网页 | 国产真实强j视频在线观看 国产真实露脸4p视频 | 黄色激情网址 | 国产在线19禁免费观看国产 | 国产亚洲精品aa在线观看 | 91久久国产 | 免费国产99久久久香蕉 | 国产三级一区 | 日韩国产精品欧美一区二区 | 国产精品黄色片 | 欧美一区二区三区在线观看 | 大学生久久香蕉国产线观看 | 国产精品无卡无在线播放 | 久久是免费只精品热在线 | 久草资源站在线 | 亚洲国产欧美一区 | 高清国产一级毛片国语 | 在线日本人观看成本人视频 | 在线观看日韩视频 | 午夜精品在线观看 | 国产精品一区欧美日韩制服 | 91精品免费观看老司机 | 国产亚洲欧美日韩国产片 | 国产伦精品一区二区三区 | 免费中日高清无专码有限公司 | 日本欧美国产精品 | 婷婷激情久久 | 老司机美女一级毛片 | 三级福利视频 | 爱爱网网站免费观看 | 六月丁香深爱六月综合激情 | 国产a视频精品免费观看 | 高清视频一区二区三区 |