最近做項目時因爲要實現防微信的底部導航欄功能,實現的方法挺多的,網上可以找到例子與源碼,在這裏我只寫出一種我用的比較好的方式,就是用fragment+radiogroup來實現底部導航欄的功能,代碼中有相關注釋,而且我會上傳源代碼(這源碼是網上下載的 嘻嘻QAQ),因此我就把佈局文件與java代碼貼出來,供大家無聊時看看
主佈局代碼 activity_main
<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"
tools:context="com.example.fragment.MainActivity" >
<FrameLayout
android:id="@+id/frame_container"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_above="@+id/div_view"
></FrameLayout>
<View
android:id="@+id/div_view"
android:layout_width="fill_parent"
android:layout_height="1.0px"
android:layout_above="@+id/main_radiogroup"
android:layout_marginBottom="2dip"
android:background="#ffc9cacb" />
<RadioGroup
android:id="@+id/main_radiogroup"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/tab_rbo_question"
style="@style/tab_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:button="@null"
android:checked="true"
android:drawableTop="@drawable/selector_tab_question"
android:text="問他"
android:textColor="@color/tv_checked_bg" />
<RadioButton
android:id="@+id/tab_rbo_message"
style="@style/tab_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:button="@null"
android:drawableTop="@drawable/selector_tab_message"
android:gravity="center_horizontal"
android:text="消息" />
<RadioButton
android:id="@+id/tab_rbo_answer"
style="@style/tab_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:button="@null"
android:drawableTop="@drawable/selector_tab_answer"
android:gravity="center_horizontal"
android:text="我要回答" />
<RadioButton
android:id="@+id/tab_rbo_discover"
style="@style/tab_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:button="@null"
android:drawableTop="@drawable/selector_tab_discover"
android:gravity="center_horizontal"
android:text="發現" />
<RadioButton
android:id="@+id/tab_rbo_user"
style="@style/tab_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@null"
android:button="@null"
android:drawableTop="@drawable/selector_tab_user"
android:gravity="center_horizontal"
android:text="我" />
</RadioGroup>
</RelativeLayout>
接下來是mainactivity
public class MainActivity extends Activity {
private RadioGroup radiogroup;
private FragmentManager fm;
private FragmentTransaction ft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radiogroup=(RadioGroup) findViewById(R.id.main_radiogroup);
fm=getFragmentManager();
radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
/** 改變文字的顏色*/
int length = group.getChildCount();
for (int i = 0; i < length; i++) {
RadioButton rgo=(RadioButton) group.getChildAt(i);
if(rgo.getId() != checkedId)
rgo.setTextColor(getResources().getColor(R.color.tv_checked_nor));
else
rgo.setTextColor(getResources().getColor(R.color.tv_checked_bg));
}
switch (checkedId) {
case R.id.tab_rbo_answer:
changeAnswer();
break;
case R.id.tab_rbo_discover:
changeDiscover();
break;
case R.id.tab_rbo_message:
changeMessage();
break;
case R.id.tab_rbo_question:
changeQuesion();
break;
case R.id.tab_rbo_user:
changeUser();
break;
}
}
});
}
private void changeQuesion() {
ft = fm.beginTransaction();
TabFragment tab1 = new TabFragment();
Bundle bundle = new Bundle();
bundle.putString("name", "問他");
tab1.setArguments(bundle);
ft.replace(R.id.frame_container, tab1);
ft.commit();
}
/**
* 切換消息
*/
private void changeMessage() {
ft = fm.beginTransaction();
TabFragment tab1 = new TabFragment();
Bundle bundle = new Bundle();
bundle.putString("name", "消息");
tab1.setArguments(bundle);
ft.replace(R.id.frame_container, tab1);
ft.commit();
}
/***
* 切換登錄
*/
private void changeUser(){
ft=fm.beginTransaction();
TabFragment tab1 = new TabFragment();
Bundle bundle = new Bundle();
bundle.putString("name", "用戶");
tab1.setArguments(bundle);
ft.replace(R.id.frame_container, tab1);
ft.commit();
}
/***
* 切換回答
*/
private void changeAnswer(){
ft=fm.beginTransaction();
TabFragment tab1 = new TabFragment();
Bundle bundle = new Bundle();
bundle.putString("name", "回答");
tab1.setArguments(bundle);
ft.replace(R.id.frame_container, tab1);
ft.commit();
}
/***
* 切換回答
*/
private void changeDiscover(){
ft=fm.beginTransaction();
TabFragment tab1 = new TabFragment();
Bundle bundle = new Bundle();
bundle.putString("name", "發現");
tab1.setArguments(bundle);
ft.replace(R.id.frame_container, tab1);
ft.commit();
}
}
上面也就是fragment的使用方法而已,其中TabFragment1就是顯示內容的fragment,可以根據要求自己添加。現在上傳源碼吧
http://download.csdn.net/detail/caohuicong/9388524