`
wangxinxin1212
  • 浏览: 29384 次
社区版块
存档分类
最新评论

使用radiobutton与fragment实现底部导航栏

阅读更多

最近在学习android,在开发中需要实现一个底部导航栏功能,与微信底部导航类似。

效果图如下:

 

网上查看了很多资料,并没有发现相关控件(可能本人是个菜鸟级别的)。

于是选用了网上较流行的一种方式实现。那就是采用radiobutton+fragment的方式实现。

原理很简单:在窗口底部放上一排radiobutton,通过StateListDrawable设置按钮状态变化的显示效果,通过监控点击按钮事件,为窗口添加fragment。

以下是主要的实现步骤:

1.窗口布局

外层采用RelativeLayout布局,内层放入一个FrameLayout与一个LinearLayout布局,LinearLayout放置在窗口底部,在其内部放入radiogroup及radiobutton

 

FrameLayout 配置代码

    <FrameLayout
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:orientation="vertical"
        android:layout_above="@+id/linearLayout"
        android:id="@+id/main_fragment">

    </FrameLayout>

 LinearLayout布局代码

<LinearLayout
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:orientation="horizontal"
        android:id="@+id/linearLayout">

 radiogroup及radiobutton部分代码

<RadioGroup
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <RadioButton//该配置来源于网上,直接使用即可
                android:id="@+id/bt_bottom_near"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom"
                android:layout_weight="1"
                android:background="@android:color/transparent"
                android:button="@null"
                android:checked="true"
                android:drawableTop="@drawable/near_bt_selector"//设置背景图片变换效果
                android:gravity="center_horizontal|bottom"
                android:paddingTop="2dp"
                android:text="@string/bt_bottom_near"
                android:textColor="@drawable/bottom_bt_text_color" />//设置字体变换效果
  ......

 near_bt_selector.xml 代码

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true"
        android:drawable="@drawable/near_active">//选中状态显示效果
    </item>
    <item android:state_checked="false"
        android:drawable="@drawable/near_black"></item>//未被选中样式效果
</selector>

 bottom_bt_text_color.xml代码

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="true" android:color="@android:color/holo_orange_dark"/>//选中颜色
    <item android:state_checked="false" android:color="@android:color/black"/>//未被选中颜色
    <item android:color="@android:color/black"/>//初始颜色

</selector>

 到这里,xml的配置都已经做好了。

2.实现打开fragment,fragment之间切换,定义按钮的click事件

主要代码如下,主要在click事件里打开一个fragment。

@Override
            public void onClick(View v) {
                MsgFragment msgFragment = new MsgFragment();//新建fragment实例
                Bundle args = new Bundle();//绑定参数
                args.putString("args", getResources().getString(R.string.bt_bottom_msg));
                msgFragment.setArguments(args);
                fragmentManager.beginTransaction().replace(R.id.main_fragment, msgFragment).commit();//记得提交
            }

 3.fragment类的实现,需要继承Fragment基类(需要注意版本问题)

主要代码

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_msg, container, false);//获得视图
        tvMsgShow = (TextView) view.findViewById(R.id.msgShow);
        tvMsgShow.setText(getArguments().getString("args"));//显示参数值
        return view;
    }

 OK,这里主要的功能都已实现。代码只提供了部分,但是相信大家还是能根据提示完成自己的功能。

 

 

 


 

  • 大小: 647.8 KB
0
0
分享到:
评论

相关推荐

    使用RadioButton+Fragment实现底部导航栏效果

    而且实现它的方式很多,今天我们就来使用RadioButton+Fragment实现底部导航栏! 下面就让我们动手吧,首先我们打开RadioButtonDemo这个项目,首先修改activity_main.xml文件如下: &lt;?xml version=1.0 encoding=...

    RadioButton_Fragment_Tab实现底部导航栏

    在以上代码中,我们完成了RadioButton与Fragment的结合,实现了底部导航栏的基本功能。然而,为了更符合Android的设计规范,通常我们会使用BottomNavigationView或者Jetpack Compose库中的TabLayout等组件来创建底部...

    Android三种实现底部导航栏样式

    2. **ViewPager+Fragment实现底部导航栏**: 这种方法利用`ViewPager`来展示多个Fragment,并通过`TabLayout`(通常与`BottomNavigationView`一起使用)来指示当前的选中状态。首先,创建包含多个Fragment的...

    Fragment+ViewPager实现底部导航栏

    现在,你已经具备了使用`Fragment`、`ViewPager`和单选按钮实现底部导航栏的基础知识。在实际项目中,你可能还需要考虑一些细节,如动画效果、数据传递等。记住,实践是最好的老师,不断尝试和优化你的代码,以提供...

    安卓APP常用界面切换RadioButton+fragment

    RadioButton通常作为底部导航栏的选项,用户可以通过选择不同的RadioButton来切换到对应的Fragment界面。下面我们将深入探讨这个主题,了解其背后的原理和实现方法。 首先,RadioButton是Android提供的一个单选按钮...

    RadioButton 底部导航栏

    本项目是利用RadioButton组件实现这样一个底部导航栏,适用于Android Studio开发环境。RadioButton通常用于单选按钮组,让用户在多个选项中选择一个。下面是关于RadioButton和底部导航栏的详细知识: 1. **...

    fragment+radioGroup实现QQ底部导航栏页面切换

    通过以上步骤,我们可以成功地使用`Fragment`和`RadioGroup`实现类似QQ底部导航栏的功能。自定义`RadioButton`可以满足特定的设计需求,使界面更加美观和一致。记住,良好的代码组织和模块化设计能帮助我们更好地...

    Android实现仿微信底部导航栏

    通过适配器(通常继承自FragmentPagerAdapter或FragmentStatePagerAdapter),我们可以将多个Fragment与ViewPager关联起来,这样当用户点击底部导航栏的某个选项时,对应的Fragment就会显示在界面上。 **RadioGroup...

    底部tab(RadioButton+fragment)

    在这个场景中,我们使用RadioButton与Fragment相结合,实现了一个动态切换内容的底部导航栏。以下是对这个技术点的详细解释。 首先,我们要理解RadioButton的基本用法。RadioButton是Android中的一个单选按钮组件,...

    android中Fragment+RadioButton实现底部导航栏

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

    底部导航栏RadioGroup + RadioButton版.rar

    虽然使用RadioGroup和RadioButton实现的底部导航栏在功能上可能稍显简陋,但通过自定义样式和动画,可以实现与BottomNavigationView类似的效果。例如,可以设置RadioButton的选中和未选中状态的背景图,以及切换时...

    使用Tablayout和RadioGroup实现底部导航

    虽然`TabLayout`更常用于底部导航,但如果你想要实现一种不依赖`ViewPager`的简单底部导航,可以考虑使用`RadioGroup`配合`RadioButton`。每个`RadioButton`代表一个页面,当用户点击某个按钮时,相应的内容区域会被...

    底部导航栏实现demo

    demo是三种实现底部导航栏的方式,第一种是LinearLayout+textView+fragment实现,第二种是RadioButton+fragement实现,第三种是使用google开发的ButtonNavigationBar+fragment实现,目前没有加入ViewPager,所以无法滑动...

    RadioGroup+ViewPager制作的底部导航栏

    本教程将探讨如何利用`RadioGroup`和`ViewPager`来实现这样一个功能丰富的底部导航栏。 首先,`RadioGroup`是Android SDK中的一个视图组,它允许你在其中放置一系列的单选按钮(RadioButton)。`RadioGroup`的主要...

    Fragment实现微信Tab主界面

    2. **底部导航栏实现**:通常使用BottomNavigationView或者自定义布局配合RadioButton等控件来实现。BottomNavigationView是Android Design Support Library的一部分,可以直接使用,它可以方便地创建符合Material ...

    fragmnet+viewPager+RadioGroup底部导航栏

    在底部导航栏中,RadioGroup用于展示各个Tab,用户点击其中一个,相应的RadioButton会被选中,同时ViewPager会切换到对应的Fragment。 构建底部导航栏的步骤如下: 1. **布局设计**:首先在XML布局文件中创建一个...

    使用ViewPager+RadioGroup+Fragment实现类似微信的底部导航

    在这个应用场景中,RadioGroup将作为底部导航栏,其中包含四个或五个RadioButton,分别代表不同的功能模块。当用户点击某个RadioButton时,对应的Fragment将在ViewPager中显示。 接下来,我们需要创建一个自定义的...

    Fragment之RadioButton

    本教程将详细讲解如何在`Fragment`中使用`RadioButton`来实现类似QQ、微信等应用底部导航栏的效果。 首先,我们需要理解`RadioButton`的基本用法。`RadioButton`属于`RadioGroup`的一部分,`RadioGroup`是一个容器...

Global site tag (gtag.js) - Google Analytics