Android进阶之路 - 通过RadioGroup + RadioButton 实现底部导航栏

阅读与使用此文知识,仅需5-20分钟

项目之初,我们都需要搭建UI界面,而底部导航的承载效果是不可或缺的,在我认知中主要有以下几种实现方式

  • Tablayout + ViewPager + Fragment
  • LinearLayout + TextView + ImageView
  • RadioGroup + RadioButton
  • BottomNavigationView + FrameLayout

对于一个项目,底部导航基本是标配,特提供我自己记录的几篇Blog

Effect

这里写图片描述
注意点:

Xml属性Icon位于文本上方

 //请注意图片大小,如果图片太大,图片本身与文本都会显示不全
 android:drawableTop="@drawable/这里调用图"

但是查询之后可能有四种方式去实现这样的一个效果,大家都可以轻松的百度到,只不过因为讲解的多而有些朋友连贯不起来,所以我特此写了一个Demo用于使用,下面进入代码时间(代码中已经注释)

MainActivity

package com.example.dow.radiogroup;

import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    private FrameLayout mFragment;
    private RadioGroup mGroup;
    private RadioButton mR1;
    private RadioButton mR2;
    private RadioButton mR3;
    private RadioButton mR4;
    private UsFragment rb1,rb2,rb3,rb4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mFragment = (FrameLayout) findViewById(R.id.fl);
        mGroup = (RadioGroup)   findViewById(R.id.ra_group);
        mR1 = (RadioButton) findViewById(R.id.ra_1);
        mR2 = (RadioButton) findViewById(R.id.ra_2);
        mR3 = (RadioButton) findViewById(R.id.ra_3);
        mR4 = (RadioButton) findViewById(R.id.ra_4);
        //设置Group监听,也就是下方的RadioButton的状态监听
        mGroup.setOnCheckedChangeListener(this);
        //初始化默认第一个RadioButton为选中状态
        mR1.setChecked(true);
    }
    
    //此下介绍:
    //rb 的变量为我们要切换的Fragment,这里因为是共用了同一个Fragment,只用于简单展示,
    // 如果在我们的项目中的话,建议创建对应的Frgament
    //问题点:
    // 有时候在走onCheckedChanged方法的hideAllFragment()方法的时候,并没有实现隐藏之前的Fragment,
    // 如果出现这个问题,尝试在else的判断中加入 hideAllFragment(transaction);方法
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        //Fragment的简单使用,获取管理者,开启事务,对应使用replace或hind,show方法
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        hideAllFragment(transaction);
        switch (checkedId){
            case R.id.ra_1:
                if(rb1==null){
                    rb1 = new UsFragment("第一个Fragment-首页");
                    transaction.add(R.id.fl,rb1);
                }else{
                    transaction.show(rb1);
                }
            break;
            case R.id.ra_2:
                if(rb2==null){
                    rb2= new UsFragment("第二个Fragment-经营");
                    transaction.add(R.id.fl,rb2);
                }else{
                    transaction.show(rb2);
                }
                break;
            case R.id.ra_3:
                if(rb3==null){
                    rb3 = new UsFragment("第三个Fragment-管理");
                    transaction.add(R.id.fl,rb3);
                }else{
                    transaction.show(rb3);
                }
                break;
            case R.id.ra_4:
                if(rb4==null){
                    rb4 = new UsFragment("第四个Fragment-中心");
                    transaction.add(R.id.fl,rb4);
                }else{
                    transaction.show(rb4);
                }
                break;
        }
        transaction.commit();
    }
    
    public void hideAllFragment(FragmentTransaction transaction){
        if(rb1!=null){
            transaction.hide(rb1);
        }
        if(rb2!=null){
            transaction.hide(rb2);
        }
        if(rb3!=null){
            transaction.hide(rb3);
        }
        if(rb4!=null){
            transaction.hide(rb4);
        }
    }
}

UsFragment(这个是我们用于切换的Fragment,在实际项目中就是几大模块):

package com.example.dow.radiogroup;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class UsFragment extends Fragment {
    private String context;
    private TextView mTV;

    public UsFragment(String context){
        this.context = context;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.us_fragment,container,false);
        mTV = (TextView)view.findViewById(R.id.textView);
        mTV.setText(context);
        return view;
    }
}

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent"
    tools:context="com.example.dow.radiogroup.MainActivity">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:id="@+id/fl"
        />
        
    <View
        android:layout_width="match_parent"
        android:layout_height="2dp"
        android:background="#2B2B2B"
        />
        
    <RadioGroup
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:id="@+id/ra_group"
        android:layout_height="70dp">

        <RadioButton
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:layout_width="0dp"
            android:button="@null"
            android:id="@+id/ra_1"
            android:drawableTop="@drawable/checket_box"
            android:gravity="center"
            android:text="首页"
            android:drawablePadding="3dp"
            android:textColor="@drawable/home_tab"
            android:layout_gravity="center"
            />
            
        <RadioButton
            android:layout_width="0dp"
            android:button="@null"
            android:layout_weight="1"
            android:gravity="center"
            android:layout_height="wrap_content"
            android:id="@+id/ra_2"
            android:text="经营"
            android:drawablePadding="3dp"
            android:drawableTop="@drawable/checket_box"
            android:layout_gravity="center"
            android:textColor="@drawable/home_tab"
            />
            
        <RadioButton
            android:layout_width="0dp"
            android:button="@null"
            android:layout_weight="1"
            android:gravity="center"
            android:text="管理"
            android:drawablePadding="3dp"
            android:layout_height="wrap_content"
            android:id="@+id/ra_3"
            android:drawableTop="@drawable/checket_box"
            android:layout_gravity="center"
            android:textColor="@drawable/home_tab"
            />
            
        <RadioButton
            android:layout_width="0dp"
            android:button="@null"
            android:drawableTop="@drawable/checket_box"
            android:layout_weight="1"
            android:text="中心"
            android:drawablePadding="3dp"
            android:gravity="center"
            android:layout_gravity="center"
            android:layout_height="wrap_content"
            android:id="@+id/ra_4"
            android:textColor="@drawable/home_tab"
            />
    </RadioGroup>
</LinearLayout>

us_fragment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:textSize="20sp"
        android:gravity="center"
        android:text="所需模块"/>

</LinearLayout>

这位朋友写的也蛮好的,大家可以借鉴下

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
### 回答1: Android底部导航栏实现可以通过使用RadioGroup和Fragment结合使用来实现。首先,在布局文件中添加一个RadioGroup,然后在其中添加多个RadioButton,每个RadioButton代表一个导航项。接着,使用Fragment来实现每个导航项对应的页面内容。在RadioGroup中设置监听器,当用户点击某个RadioButton时,切换到对应的Fragment页面。这样就可以实现一个简单的底部导航栏了。 ### 回答2: Android底部导航栏实现一般可以通过RadioGroup和Fragment结合使用来完成。首先,在XML布局文件中定义一个RadioGroup,并在其中添加多个RadioButton,每个RadioButton对应导航栏中的一个选项。 接下来,在Activity中,我们需要定义一个Fragment的容器,用于加载不同的Fragment页面。在RadioGroup的选择监听器中,根据所选中的RadioButton的id,切换对应的Fragment页面。 在切换Fragment页面时,可以使用FragmentManager和FragmentTransaction来实现。通过FragmentManager的beginTransaction()方法获取一个FragmentTransaction的实例,然后使用replace()方法将容器中的Fragment替换为选中的目标Fragment,最后通过commit()方法提交事务即可完成页面切换。 为了方便管理和切换Fragment,可以定义一个Fragment的集合,用于存放所有的Fragment实例,并在选择监听器中根据RadioButton的顺序获取对应的Fragment。 另外,为了保持Fragment的状态,在切换Fragment时可以使用hide()和show()方法而不是replace()来隐藏和显示Fragment,这样可以避免Fragment被重复实例化。 总结来说,通过RadioGroup和Fragment的结合使用,我们可以实现Android底部导航栏的功能。通过监听RadioGroupRadioButton的选择事件,来切换不同的Fragment页面。这样可以实现底部导航栏的选项切换,同时保持Fragment的状态。 ### 回答3: Android底部导航栏实现可以通过使用RadioGroup和Fragment来实现。 首先,我们可以在布局文件中创建一个包含多个RadioButtonRadioGroup,这些RadioButton将作为底部导航栏的按钮。我们可以为每个RadioButton设置图标和文本,以表示不同的导航选项。通过设置RadioGroup的布局属性可以将其放置在屏幕的底部。 接下来,我们需要创建对应的Fragment,并在Activity中使用FragmentTransaction来进行Fragment的切换。在FragmentTransaction中,我们可以通过调用replace方法来替换Activity中的显示内容,将选中的RadioButton对应的Fragment显示出来。 当用户点击底部导航栏RadioButton时,我们可以通过设置RadioGroup的OnCheckedChangeListener来监听选中项的变化。当选中项发生变化时,我们可以获取选中的RadioButton对应的Fragment,并使用FragmentTransaction来进行Fragment的切换,从而显示选中的Fragment。 此外,为了更好地控制底部导航栏的切换效果,我们还可以使用fragment的缓存机制以提高切换的效率,避免每次切换都重新创建Fragment对象。 总之,通过结合RadioGroup和Fragment的使用,我们可以方便地实现Android底部导航栏的功能,使用户可以方便地在不同的导航选项之间进行切换。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

远方那座山

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值