目录:

一、概述

最近在做一个新闻类结合社区的APP的时候,需要添加一个侧滑菜单的效果,考虑到可以使用DrawerLayout布局,但是问题是使用了 DrawerLayout布局后,主页内容应该是一个Fragment类,类似QQ主页的效果,那么问题来了,如何在主页的底部添加TabHost导航的 效果呢?之前使用过FragmentTabHost,在我的另一篇文章里,点击这里查看,在一个DrawerLayout里面再使用一个FragmentTabHost添加TabHost导航效果,经过测试,发觉不行,于是考虑使用RadioGroupRadioButton嵌套到Fragment里面,加载页面的时候,我们可以通过RadioButton选项,切换对应的Fragment,效果图:

二、认识RadioGroup和RadioButton

RadioButton在做表单的时候经常用到,在安卓开发中,RadioButton需要和RadioGroup一起使用,表示在一组可选项中,
只有一个可以被选中,RadioGroup状态改变的一个监视器OnCheckedChangeListener,RadioGroup使用的时候调用
setOnCheckedChangeListener(),然后重写OnCheckedChangeListener中的
onCheckedChanged()方法,比如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener(){
           @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // 获取变更后的选项的ID
        int radioButtonId = group.getCheckedRadioButtonId();
        switch (radioButtonId) {
        case R.id.message_radiobtn:
            mFragment = new MessageFragment();
            break;
        case R.id.contact_radiobtn:
            mFragment = new ContactFragment();
            break;
        case R.id.dynamic_radiobtn:
            mFragment = new DynamicFragment();
            break;
        default:
            break;
        }
        getActivity().getSupportFragmentManager().beginTransaction()
                .replace(R.id.realtabcontent, mFragment).commit();
    }
});

OnCheckedChangeListener(RadioGroup,int)中有两个参数,RadioGroup表示当前哪个选项组,指定选项组下的选项,int表示是否选中状态。

三、自定义RadioButton

RadioButton默认状态下,前面带一个小圆点,文字在小圆点的右边,同时设置的图片也不是刚好在文字上面,而我们的TabHost导航中不
需要小圆点,如何去掉小圆点并让文字居中显示,我们可以自定义一个样式,命名:tabstyle,然后我们直接在RadioButton中引用即可,如下
图:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    <style name="tabstyle">
        <item name="android:button">@null</item>
        <item name="android:textColor">@color/nav_text_color_selec</item>
        <item name="android:gravity">center</item>
    </style>
 
<!--下面是引用自定义样式的布局文件-->
<?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" >
 
    <FrameLayout
        android:id="@+id/realtabcontent"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/main_radiogroup"
        android:layout_alignParentTop="true"/>
 
    <RadioGroup
        android:id="@+id/main_radiogroup"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_gravity="bottom"
        android:gravity="bottom"
        android:orientation="horizontal" >
 
        <RadioButton
            android:id="@+id/message_radiobtn"
            android:layout_weight="1"
            android:checked="true"
            android:drawableTop="@drawable/community_main_btn"
            android:text="消息"
            style="@style/tabstyle"/>
 
        <RadioButton
            android:id="@+id/contact_radiobtn"
            android:layout_weight="1"
            android:drawableTop="@drawable/goverment_main_btn"
            android:text="联系人"
            style="@style/tabstyle"/>
 
        <RadioButton
            android:id="@+id/dynamic_radiobtn"
            android:layout_weight="1"
            android:drawableTop="@drawable/news_main_btn"
            android:text="动态"
            style="@style/tabstyle"/>
 
    </RadioGroup>
 
</RelativeLayout>

四、测试

1、在我的案例里面,主布局文件(activity_drawer_layout.xml)使用DrawerLayout,然后添加策划的一个Fragment,在FrameLayout中加载单选按钮选中的Fragment,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/id_drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="cn.teachcourse.www.DrawerLayoutActivity" >
 
    <!-- FrameLayout布局用于展示内容 -->
 
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent" >
    </FrameLayout>
 
    <!-- 直接添加侧滑菜单的MenuFragment到布局中 -->
 
    <fragment
        android:id="@+id/id_left_menu"
        android:name="cn.teachcourse.www.ui.main.MenuFragment"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:tag="LEFT" />
 
</android.support.v4.widget.DrawerLayout>

2、下面开始介绍Activity的编程,在主Activity中还算是比较简单,加载activity_drawer_layout.xml布局文件,然后获取DrawerLayout控件,控制策滑Fragment的开与关,这里可以先查以下资料,主要代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_drawer_layout);
 
        // 从保存中状态中获取实例化的Fragment对象
        if (savedInstanceState != null) {
            mContent = (Fragment) getSupportFragmentManager().getFragment(
                    savedInstanceState, "mContent");
        }
 
        if (mContent == null) {
 
            mContent = new ContentFragment();
            mFragment=new MainTabHostActivity();
 
        }
        // 开启事务,替换,提交
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.content_frame, mContent).commit();
        // 初始化控件
        initView();
        // 添加DrawerLayout监视器事件setDrawerListener
        initEvents();
    }
    // 当程序退出的时候,保存状态
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        getSupportFragmentManager().putFragment(outState, "mContent", mContent);
    }
 
    // 开启菜单
    public void openMenu() {
        flage = true;
        mDrawerLayout.openDrawer(Gravity.LEFT);
        mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED,
                Gravity.RIGHT);
 
    }
 
    // 关闭菜单
    public void closeMenu() {
        // TODO Auto-generated method stub
        mDrawerLayout.closeDrawers();
        flage = false;
 
    }

3、在单选项中,我们定义了三个Fragment,分别为:MessageFragment,ContactFragment和DynamicFragment,在三个Fragment中都放置一个ImageView控件,设置一张图片效果演示,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<!--message_fragment.xml-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
 
<ImageView
    android:src="@drawable/qq_message"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitXY"/>
 
</LinearLayout>

同理,另外的ContactFragment、DynamicFragment布局一样,把src图片换了就行。
最终测试,效果图如下:

五、参考链接

转载转自TeachCourse博客:TeachCourse博客-做最好的安卓源码分析网站

如何使用RadioGroup和RadioButton实现FragmentTabHost导航效果?的更多相关文章

  1. Android下利用RadioGroup和RadioButton实现Tabbar的效果

    本实现方法主要使用RadioGroup和RadioButton的组合方式来实现Tabbar的效果. 其中选中的Tab的切换的动作可以通过RadioGroup的OnCheckedChangeListen ...

  2. 二、Fragment+RadioButton实现底部导航栏

    在App中经常看到这样的tab底部导航栏   那么这种效果是如何实现,实现的方式有很多种,最常见的就是使用Fragment+RadioButton去实现.下面我们来写一个例子 首先我们先在activi ...

  3. Android入门(八):使用RadioGroup 和RadioButton组件建立单选清单

    这一章,我们学习RadioGroup 和RadioButton组件,我们新建一个项目,编码过程与前几章的项目类似. 1.建立字符串资源文件strings.xml: <resources> ...

  4. Android控件之RadioGroup与RadioButton(单选控件)

    一.RadioGroup与RadioButton 1.什么是RadioGroup: RadioButton的一个集合,提供多选机制 2.什么是RadioButton: RadioButton包裹在Ra ...

  5. 使用RadioGroup与RadioButton实现多选一

    RadioGroup是RadioButton的集合, RadioGroup里面可以包含很多RadioButton,提供多选一机制,只能选择其中一个 RadioGroup的orientation(方向) ...

  6. React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton)

    React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton) 一,需求与简单介绍 在开发项目时发现RN没有给提供RadioButton和Rad ...

  7. 动态布局中RadioGroup的RadioButton有时候不相互排斥的原因

    近期在做一个答题类的模块,有单选.简答.调查问卷等,我是用动态布局的方式生成答题项的.在弄单选的时候遇到一个比較奇葩的问题,在代码中生成RadioGroup和RadioButton的时候.会发现不能相 ...

  8. Android 使用RadioGroup和RadioButton实现单选效果

    RadioButton和CheckBox的区别:CheckBox选中之后可以直接取消,RadioButton选中之后不能直接取消,所以一般情况下不建议单独使用.1.RadioGroup:RadioBu ...

  9. 简述RadioGroup和RadioButton的使用

    简述RadioGroup和RadioButton的使用 在项目中有身份选择的选项,需要用到RadioGroup和RadioButton,这里做个记录,和大家交流一下. 话不多说,一起看代码 XML代码 ...

随机推荐

  1. luogu P3092 [USACO13NOV]没有找零No Change

    题目描述 Farmer John is at the market to purchase supplies for his farm. He has in his pocket K coins (1 ...

  2. saltstack安装+基本命令

    环境: node1:172.16.1.60 OS:centos 7.3 master hostname:centos7u3-1 node2:172.16.1.61 OS:centos 7.3 mini ...

  3. hosts文件中同一个域名两个IP的解析顺序

    比如: 192.168.0.2 www.easonjim.com 192.168.0.3 www.easonjim.com 那么解析顺序就只有最开头的IP,即:192.168.0.2 经过测试,相同域 ...

  4. 初学React,setState后获取到的thisstate没变,还是初始state?

    问题:(javascript)初学React,setState后获取到的thisstate没变,还是初始state?描述: getInitialState(){ return {data:[]}; } ...

  5. Using Single Alert For Messages And Confirmation Messages In Oracle Forms With Set_Alert_Button_Property

    Learn how to use single Oracle Form's Alert object for warning/information messages and confirmation ...

  6. 说说GestureDetector.OnGestureListener onScroll函数

    public abstract boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) ...

  7. su、sudo、sudo su、sudo -i的用法和区别

    sudo : 暂时切换到超级用户模式以执行超级用户权限,提示输入密码时该密码为当前用户的密码,而不是超级账户的密码.不过有时间限制,Ubuntu默认为一次时长15分钟.su : 切换到某某用户模式,提 ...

  8. 【2048小游戏】——原生js爬坑之封装行的移动算法&事件

    引言:2048小游戏的核心玩法是移动行,包括横行和纵行,玩家可以选择4个方向,然后所有行内的数字就会随着行的移动而向特定的方向移动.这个行的移动是一个需要重复调用的算法,所以这里就要将一行的移动算法封 ...

  9. mongodb配置副本集(多台服务器间的副本集搭建) replica[ˈrɛplɪkə]

    副本集具有多个副本保证了容错性,就算一个副本挂掉了还有很多副本存在,并且解决了“主节点挂掉了,整个集群内会自动切换”的问题.我们来看看mongoDB副本集的架构图: 由图可以看到客户端连接到整个副本集 ...

  10. 怎样通过Html网页调用本地安卓app

    怎样使用html网页和本地app进行传递数据呢?经过研究.发现还是有方法的,总结了一下,大致有一下几种方式 一.通过html页面打开Android本地的app 1.首先在编写一个简单的html页面 & ...