服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - 使用RadioButton+Fragment实现底部导航栏效果

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

2022-03-03 14:52JiangWeHu Android

这篇文章主要为大家详细介绍了使用RadioButton+Fragment实现底部导航栏效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

底部导航栏,在我们App项目中是非常常用!而且实现它的方式很多,今天我们就来使用RadioButton+Fragment实现底部导航栏!

下面就让我们动手吧,首先我们打开RadioButtonDemo这个项目,首先修改activity_main.xml文件如下:

?
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical"
 tools:context="com.example.jackhu.radiobuttondemo.MainActivity">
 
 <FrameLayout
  android:id="@+id/mFragment"
  android:layout_width="match_parent"
  android:layout_height="0dp"
  android:layout_weight="1"></FrameLayout>
 
 <RadioGroup
  android:layout_marginBottom="2dp"
  android:id="@+id/mRadioGroup"
  android:orientation="horizontal"
  android:layout_width="match_parent"
  android:layout_height="48dp">
  <RadioButton
   android:drawableTop="@drawable/rbhome"
   android:button="@null"
   android:checked="true"
   android:textColor="@color/colorRadioButtonP"
   android:id="@+id/mRb_home"
   android:gravity="center"
   android:layout_width="0dp"
   android:text="Home"
   android:layout_weight="1"
   android:layout_height="match_parent" />
 
  <RadioButton
   android:drawableTop="@drawable/rb_message"
   android:button="@null"
   android:textColor="@color/colorRadioButtonN"
   android:id="@+id/mRb_message"
   android:gravity="center"
   android:layout_width="0dp"
   android:text="Message"
   android:layout_weight="1"
   android:layout_height="match_parent" />
 
  <RadioButton
   android:drawableTop="@drawable/rbfind"
   android:button="@null"
   android:textColor="@color/colorRadioButtonN"
   android:id="@+id/mRb_find"
   android:gravity="center"
   android:layout_width="0dp"
   android:text="Find"
   android:layout_weight="1"
   android:layout_height="match_parent" />
 
  <RadioButton
   android:drawableTop="@drawable/rbmy"
   android:button="@null"
   android:textColor="@color/colorRadioButtonN"
   android:id="@+id/mRb_my"
   android:gravity="center"
   android:layout_width="0dp"
   android:text="My"
   android:layout_weight="1"
   android:layout_height="match_parent" />
 
 </RadioGroup>
 
</LinearLayout>

这里我们在布局文件Fragment控件:用于显示界面的切换。

RadioGroup控件包含了4个RadioButton:用于显示按钮。我们给第一个按钮check为true默认选中。其中android:button=”@null” 取消圆点。

drawableTop属性:

?
1
2
3
4
5
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_checked="true" android:drawable="@drawable/home_p"/>
 <item android:drawable="@drawable/home_n"/>
</selector>

显示选择和未选中的状态的图标

创建Fragment,加载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
package com.example.jackhu.radiobuttondemo.fragment;
 
 
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
 
import com.example.jackhu.radiobuttondemo.R;
 
/**
 * A simple {@link Fragment} subclass.
 */
public class HomeFragment extends Fragment {
 
 
 public HomeFragment() {
  // Required empty public constructor
 }
 
 //单例模式
 public static HomeFragment newInstance(){
  HomeFragment homeFragment=new HomeFragment();
  return homeFragment;
 }
 
 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
  // Inflate the layout for this fragment
  return inflater.inflate(R.layout.fragment_home, container, false);
 }
 
}

接下来我们来修改MainActivity.class中的代码,在这里实现点击按钮切换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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.example.jackhu.radiobuttondemo;
 
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
 
import com.example.jackhu.radiobuttondemo.fragment.FindFragment;
import com.example.jackhu.radiobuttondemo.fragment.HomeFragment;
import com.example.jackhu.radiobuttondemo.fragment.MessageFragment;
import com.example.jackhu.radiobuttondemo.fragment.MyFragment;
 
import java.util.ArrayList;
import java.util.List;
 
public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
 
 private RadioGroup mRadioGroup;
 private List<Fragment> fragments = new ArrayList<>();
 private Fragment fragment;
 private FragmentManager fm;
 private FragmentTransaction transaction;
 private RadioButton rb_Home,rb_Message,rb_Find,rb_My;
 
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  initView(); //初始化组件
  mRadioGroup.setOnCheckedChangeListener(this); //点击事件
  fragments = getFragments(); //添加布局
  //添加默认布局
  normalFragment();
 }
 
 //默认布局
 private void normalFragment() {
  fm=getSupportFragmentManager();
  transaction=fm.beginTransaction();
  fragment=fragments.get(0);
  transaction.replace(R.id.mFragment,fragment);
  transaction.commit();
 }
 
 private void initView() {
  mRadioGroup = (RadioGroup) findViewById(R.id.mRadioGroup);
  rb_Home= (RadioButton) findViewById(R.id.mRb_home);
  rb_Message= (RadioButton) findViewById(R.id.mRb_message);
  rb_Find= (RadioButton) findViewById(R.id.mRb_find);
  rb_My= (RadioButton) findViewById(R.id.mRb_my);
 }
 
 @Override
 public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
  fm=getSupportFragmentManager();
  transaction=fm.beginTransaction();
  switch (checkedId){
   case R.id.mRb_home:
    fragment=fragments.get(0);
    transaction.replace(R.id.mFragment,fragment);
    Toast.makeText(this, "Home", Toast.LENGTH_SHORT).show();
    break;
   case R.id.mRb_message:
    fragment=fragments.get(1);
    transaction.replace(R.id.mFragment,fragment);
    Toast.makeText(this, "Message", Toast.LENGTH_SHORT).show();
    break;
   case R.id.mRb_find:
    fragment=fragments.get(2);
    transaction.replace(R.id.mFragment,fragment);
    Toast.makeText(this, "Find", Toast.LENGTH_SHORT).show();
    break;
   case R.id.mRb_my:
    fragment=fragments.get(3);
    transaction.replace(R.id.mFragment,fragment);
    Toast.makeText(this, "My", Toast.LENGTH_SHORT).show();
    break;
  }
  setTabState();
  transaction.commit();
 }
 
 //设置选中和未选择的状态
 private void setTabState() {
  setHomeState();
  setMessageState();
  setFindState();
  setMyState();
 }
 
 private void setMyState() {
  if (rb_My.isChecked()){
   rb_My.setTextColor(ContextCompat.getColor(this,R.color.colorRadioButtonP));
  }else{
   rb_My.setTextColor(ContextCompat.getColor(this,R.color.colorRadioButtonN));
  }
 }
 
 private void setFindState() {
  if (rb_Find.isChecked()){
   rb_Find.setTextColor(ContextCompat.getColor(this,R.color.colorRadioButtonP));
  }else{
   rb_Find.setTextColor(ContextCompat.getColor(this,R.color.colorRadioButtonN));
  }
 }
 
 private void setMessageState() {
  if (rb_Message.isChecked()){
   rb_Message.setTextColor(ContextCompat.getColor(this,R.color.colorRadioButtonP));
  }else{
   rb_Message.setTextColor(ContextCompat.getColor(this,R.color.colorRadioButtonN));
  }
 }
 
 private void setHomeState() {
  if (rb_Home.isChecked()){
   rb_Home.setTextColor(ContextCompat.getColor(this,R.color.colorRadioButtonP));
  }else{
   rb_Home.setTextColor(ContextCompat.getColor(this,R.color.colorRadioButtonN));
  }
 }
 
 public List<Fragment> getFragments() {
  fragments.add(new HomeFragment());
  fragments.add(new MessageFragment());
  fragments.add(new FindFragment());
  fragments.add(new MyFragment());
  return fragments;
 }
}

好了,这样的话,所有的代码就已经完成了,可以运行一下看看完整的效果了最终效果图:

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/github_37216983/article/details/68628540

延伸 · 阅读

精彩推荐
  • AndroidAndroid程序设计之AIDL实例详解

    Android程序设计之AIDL实例详解

    这篇文章主要介绍了Android程序设计的AIDL,以一个完整实例的形式较为详细的讲述了AIDL的原理及实现方法,需要的朋友可以参考下...

    Android开发网4642021-03-09
  • AndroidAndroid界面效果UI开发资料汇总(附资料包)

    Android界面效果UI开发资料汇总(附资料包)

    android ui界面设计,友好的界面会提高用户体验度;同时也增强了android ui界面设计的难度,本文提供了一些常用开发资料(有下载哦)感兴趣的朋友可以了解下...

    Android开发网4672021-01-03
  • AndroidAndroid CardView+ViewPager实现ViewPager翻页动画的方法

    Android CardView+ViewPager实现ViewPager翻页动画的方法

    本篇文章主要介绍了Android CardView+ViewPager实现ViewPager翻页动画的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    Abby代黎明9602022-03-02
  • AndroidAndroid中AsyncTask详细介绍

    Android中AsyncTask详细介绍

    这篇文章主要介绍了Android中AsyncTask详细介绍,AsyncTask是一个很常用的API,尤其异步处理数据并将数据应用到视图的操作场合,需要的朋友可以参考下...

    Android开发网7452021-03-11
  • AndroidAndroid编程解析XML方法详解(SAX,DOM与PULL)

    Android编程解析XML方法详解(SAX,DOM与PULL)

    这篇文章主要介绍了Android编程解析XML方法,结合实例形式详细分析了Android解析XML文件的常用方法与相关实现技巧,需要的朋友可以参考下...

    liuhe68810052021-05-03
  • Android汇总Android视频录制中常见问题

    汇总Android视频录制中常见问题

    这篇文章主要汇总了Android视频录制中常见问题,帮助大家更好地解决Android视频录制中常见的问题,需要的朋友可以参考下...

    yh_thu5192021-04-28
  • AndroidAndroid实现固定屏幕显示的方法

    Android实现固定屏幕显示的方法

    这篇文章主要介绍了Android实现固定屏幕显示的方法,实例分析了Android屏幕固定显示所涉及的相关技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    鉴客6192021-03-27
  • AndroidAndroid实现Service获取当前位置(GPS+基站)的方法

    Android实现Service获取当前位置(GPS+基站)的方法

    这篇文章主要介绍了Android实现Service获取当前位置(GPS+基站)的方法,较为详细的分析了Service基于GPS位置的技巧,具有一定参考借鉴价值,需要的朋友可以参考下...

    Ruthless8342021-03-31