利用fragment+radiogroup实现底部标题栏
2015-12-28 10:09
1361 查看
最近做项目时因为要实现防微信的底部导航栏功能,实现的方法挺多的,网上可以找到例子与源码,在这里我只写出一种我用的比较好的方式,就是用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
主布局代码 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
相关文章推荐
- Android调试桥
- HDU ACM 1290 献给杭电五十周年校庆的礼物
- 全球四大国际反垃圾邮件组织介绍
- 通过HTTP协议上传文件
- 让Label竖着显示
- httpclient发送post请求
- OpenCV参考手册之CvCapture结构体
- Redis学习笔记(一) 初识 Redis
- Android之NDK开发
- 偏执却管用的10条Java编程技巧
- 原生JavaScript实现瀑布流布局
- 如何为PHP贡献代码
- 程序员也要写文章:好处不止多赚钱
- 写于2015年末的总结
- 如何为PHP贡献代码
- libcurl
- 浏览器 HTTP 协议缓存机制详解
- 明年工资新规定,8种人将被淘汰,你在其中么?
- 每日一vim(14)tab标签页
- InitialContext和lookup