您好,登錄后才能下訂單哦!
Android中怎么利用RadioGroup實(shí)現(xiàn)底部導(dǎo)航欄,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
布局:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/fragment_content"/> <View android:layout_width="match_parent" android:layout_height="1px" android:background="@color/grey_300" android:elevation="20dp"/> <RadioGroup android:id="@+id/radio_group" android:layout_width="match_parent" android:layout_height="56dp" android:layout_alignParentBottom="true" android:background="@color/white" android:orientation="horizontal"> <RadioButton android:id="@+id/rb_home" style="@style/radiobutton_style" android:checked="true" android:drawableTop="@drawable/radiobutton_bg_home" android:text="@string/item_home" /> <RadioButton android:id="@+id/rb_location" style="@style/radiobutton_style" android:drawableTop="@drawable/radiobutton_bg_location" android:text="@string/item_location"/> <RadioButton android:id="@+id/rb_like" style="@style/radiobutton_style" android:drawableTop="@drawable/radiobutton_bg_like" android:text="@string/item_like"/> <RadioButton android:id="@+id/rb_me" style="@style/radiobutton_style" android:drawableTop="@drawable/radiobutton_bg_me" android:text="@string/item_person"/> </RadioGroup> </RelativeLayout>
這里的drawableTop使用了狀態(tài)選擇器
<selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/home_fill" android:state_checked="true"/> <item android:drawable="@drawable/home"/> </selector>
style
<style name="radiobutton_style"> <item name="android:layout_width">0dp</item> <item name="android:padding">3dp</item> <item name="android:layout_height">match_parent</item> <item name="android:layout_weight">1</item> <item name="android:button">@null</item><!--去除RadioButton默認(rèn)帶的圓點(diǎn)--> <item name="android:gravity">center</item> <item name="android:textSize">12sp</item> </style>
代碼
初始化的代碼就不記錄了,都是一些findViewById,實(shí)現(xiàn)過(guò)程無(wú)非就是對(duì)RadioButton進(jìn)行監(jiān)聽(tīng)一下:
mRadioGroup.setOnCheckedChangeListener(this); @Override public void onCheckedChanged(RadioGroup group, int checkId) { FragmentTransaction transaction = getFragmentManager().beginTransaction(); switch (checkId) { case R.id.rb_home: if (mHomeFragment == null) { mHomeFragment = HomeFragment.newInstance(getString(R.string.item_home)); } transaction.replace(R.id.sub_content, mHomeFragment); break; case R.id.rb_location: if (mLocationFragment == null) { mLocationFragment = LocationFragment.newInstance(getString(R.string.item_location)); } transaction.replace(R.id.sub_content, mLocationFragment); break; case R.id.rb_like: if (mLikeFragment == null) { mLikeFragment = LikeFragment.newInstance(getString(R.string.item_like)); } transaction.replace(R.id.sub_content, mLikeFragment); break; case R.id.rb_me: if (mPersonFragment == null) { mPersonFragment = PersonFragment.newInstance(getString(R.string.item_person)); } transaction.replace(R.id.sub_content, mPersonFragment); break; } setTabState();//設(shè)置狀態(tài) transaction.commit(); }
狀態(tài)的設(shè)置
private void setTabState() { setHomeState(); setLocationState(); setLikeState(); setMeState(); } /** * set tab home state */ private void setHomeState() { if (mRadioHome.isChecked()) { mRadioHome.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary)); } else { mRadioHome.setTextColor(ContextCompat.getColor(getActivity(), R.color.black)); } } private void setLocationState() { if (mRadioLocation.isChecked()) { mRadioLocation.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary)); } else { mRadioLocation.setTextColor(ContextCompat.getColor(getActivity(), R.color.black)); } } private void setLikeState() { if (mRadioLike.isChecked()) { mRadioLike.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary)); } else { mRadioLike.setTextColor(ContextCompat.getColor(getActivity(), R.color.black)); } } private void setMeState() { if (mRadioMe.isChecked()) { mRadioMe.setTextColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary)); } else { mRadioMe.setTextColor(ContextCompat.getColor(getActivity(), R.color.black)); } }
這里需要注意的是, setDefaultFragment();我寫(xiě)在onCreateVew里面并沒(méi)有生效。這里我寫(xiě)在了onStart()方法里了。
@Override public void onStart() { setDefaultFragment();//寫(xiě)在onCreateView里面,當(dāng)頁(yè)面跑到其他Fragment再回來(lái)就不會(huì)生效 super.onStart(); }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。