這裡簡單記錄一下android底部導航欄通過radiogroup+fragment的實作。
布局:
<?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_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"
/>
android:id="@+id/rb_location"
android:drawabletop="@drawable/radiobutton_bg_location"
android:text="@string/item_location"/>
android:id="@+id/rb_like"
android:drawabletop="@drawable/radiobutton_bg_like"
android:text="@string/item_like"/>
android:id="@+id/rb_me"
android:drawabletop="@drawable/radiobutton_bg_me"
android:text="@string/item_person"/>
</radiogroup>
</relativelayout>
這裡的drawabletop使用了狀态選擇器
<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預設帶的圓點-->
<item name="android:gravity">center</item>
<item name="android:textsize">12sp</item>
</style>
代碼
初始化的代碼就不記錄了,都是一些findviewbyid,實作過程無非就是對radiobutton進行監聽一下:
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);
case r.id.rb_like:
if (mlikefragment == null) {
mlikefragment = likefragment.newinstance(getstring(r.string.item_like));
transaction.replace(r.id.sub_content, mlikefragment);
case r.id.rb_me:
if (mpersonfragment == null) {
mpersonfragment = personfragment.newinstance(getstring(r.string.item_person));
transaction.replace(r.id.sub_content, mpersonfragment);
}
settabstate();//設定狀态
transaction.commit();
}
狀态的設定
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));
mradiolocation.settextcolor(contextcompat.getcolor(getactivity(), r.color.black));
private void setlikestate() {
if (mradiolike.ischecked()) {
mradiolike.settextcolor(contextcompat.getcolor(getactivity(), r.color.colorprimary));
mradiolike.settextcolor(contextcompat.getcolor(getactivity(), r.color.black));
private void setmestate() {
if (mradiome.ischecked()) {
mradiome.settextcolor(contextcompat.getcolor(getactivity(), r.color.colorprimary));
mradiome.settextcolor(contextcompat.getcolor(getactivity(), r.color.black));
這裡需要注意的是, setdefaultfragment();我寫在oncreatevew裡面并沒有生效。這裡我寫在了onstart()方法裡了。
@override
public void onstart() {
setdefaultfragment();//寫在oncreateview裡面,當頁面跑到其他fragment再回來就不會生效
super.onstart();
}
說明:這幾篇文章沒有過多的文字叙述,因為這些東西也不是很難,而且都是常用的,相信很多人都了如指掌了,多說亦是廢話,直接上代碼看的反而更清楚。
作者:k_j
來源:51cto