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


    本实现方法主要使用RadioGroup和RadioButton的组合方式来实现Tabbar的效果。

    其中选中的Tab的切换的动作可以通过RadioGroup的OnCheckedChangeListener监听事件来完成动作的响应。

    tab切换事件代码如下:

    RadioGroup rg = (RadioGroup) findViewById(R.id.bottom_tabbar);
    rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(RadioGroup group, int checkedId) {
                    switch (checkedId) {
                    case R.id.bottom_tabbar_rb_1:
                        …
                        break;
                    case R.id.bottom_tabbar_rb_2:
                        …
                        break;
                    }
                }
            });
    

     如果要设置初始的选中item可以使用RaidoGroup的check(int id)方法。参数id指的是RadioGroup中的RadioButton的id。

    tabbar对应的xml的布局文件如下:

    <?xml version="1.0" encoding="utf-8"?>
    <RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="@dimen/bottom_bar_height"    android:orientation="horizontal" ><!—设置横向排列 -->
    
        <RadioButton
            android:id="@+id/bottom_tabbar_rb_1"
           android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/bg_bottom_bar_item"<!—设置Tab的选中背景-->
            android:button="@android:color/transparent"<!—隐藏RaidoButton的图标-->
            android:drawableTop="@drawable/ic_bottom_item_home"<!—设置RadioButton的Icon-->
            android:gravity="center"
            android:text="@string/bottom_bar_item_home_text"/>
    
        <RadioButton
            android:id="@+id/bottom_tabbar_rb_2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="@drawable/bg_bottom_bar_item"
            android:button="@android:color/transparent"
            android:drawableTop="@drawable/ic_bottom_item_notice"
            android:gravity="center"
            android:text="@string/bottom_bar_item_notice_text"/>
    </RadioGroup>
    

     显示的效果如下:

  • 相关阅读:
    对.Net Framework的认识(3)
    对负载均衡的认识
    对.Net Framework的认识(1)
    对.Net Framework的认识(2)
    对ASP.Net的认识(二)
    windows 使用 ssh 隧道代理
    python + django 搭建网页(尝试4):自动显示txt文件
    群论基础(3):有限群表示论
    群论基础(1):群的定义
    群论基础(5):李群
  • 原文地址:https://www.cnblogs.com/janken/p/3727728.html
Copyright © 2020-2023  润新知