Android Fragment实现底部通知栏
作者:一位没有感情的安卓君 时间:2023-12-25 21:01:33
Android Fragment实现底部通知栏,供大家参考,具体内容如下
截图如下:
1. 第一步先要创建fragment(动态注册)
然后将两个勾选取消掉(还有一种是自己手动创建)
会自动生成相对应的layout布局,剩下的要根据自己的需求了
2.在Activity的布局里写好四个按钮
这里不是重点…
<?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"
>
<LinearLayout
android:id="@+id/ll_content_part"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</LinearLayout>
<LinearLayout
android:id="@+id/ll_function"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_msg_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="13dp"
android:onClick="click"
android:text="msg"/>
<Button
android:id="@+id/btn_contact"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="13dp"
android:onClick="click"
android:text="contact"/>
<Button
android:id="@+id/btn_disc"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="13dp"
android:onClick="click"
android:text="disc"/>
<Button
android:id="@+id/btn_me"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="13dp"
android:onClick="click"
android:text="me"/>
</LinearLayout>
</RelativeLayout>
3.Activity的代码
其中定义了四个整型常量记录了四个按钮的状态,还有一个当前状态,进而判断当前点击按钮的状态,点击切换文字颜色和图标
每次判断四个Fragment的引用是否为空,不为空就不需要每次在new一遍Fragment
replace每次都会重新初始化fragment,它是先remove掉相同id的fragment,再add当前fragment。
add不会回每次都初始化fragment,一般配合hide()和show()方法
public class MainActivity extends AppCompatActivity {
private Button btn_contact;
private Button btn_disc;
private Button btn_me;
private Button btn_msg_list;
private FragmentManager fragmentManager;
private MsgListFragment msgListFragment;
private ContactFragment contactFragment;
private DiscoveryFragment discoveryFragment;
private MeFragment meFragment;
private final int STATE_MSG =1;
private final int STATE_CON =2;
private final int STATE_DIS =3;
private final int STATE_ME =4;
private int currentState;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
chageButtonColor(currentState,STATE_MSG);
}
private void initData() {
fragmentManager = getFragmentManager();
switchFragment(this.STATE_MSG);
}
private void initView() {
btn_contact = (Button) findViewById(R.id.btn_contact);
btn_disc = (Button) findViewById(R.id.btn_disc);
btn_me = (Button) findViewById(R.id.btn_me);
btn_msg_list = (Button) findViewById(R.id.btn_msg_list);
}
public void click(View view) {
switch (view.getId()){
case R.id.btn_msg_list:
switchFragment(this.STATE_MSG);
break;
case R.id.btn_contact:
switchFragment(this.STATE_CON);
break;
case R.id.btn_disc:
switchFragment(this.STATE_DIS);
break;
case R.id.btn_me:
switchFragment(this.STATE_ME);
break;
}
}
private void switchFragment(int toState) {
if (toState == currentState)
return;
chageButtonColor(currentState,toState);
this.currentState=toState;
FragmentTransaction transaction = this.fragmentManager.beginTransaction();
Fragment fragment=msgListFragment;
switch (toState){
case STATE_DIS:
if (discoveryFragment == null)
discoveryFragment= new DiscoveryFragment();
fragment= discoveryFragment;
break;
case STATE_ME:
if (meFragment == null)
meFragment= new MeFragment();
fragment= meFragment;
break;
case STATE_CON:
if (contactFragment == null)
contactFragment= new ContactFragment();
fragment= contactFragment;
break;
case STATE_MSG:
if (msgListFragment == null)
msgListFragment= new MsgListFragment();
fragment= msgListFragment;
break;
}
transaction.replace(R.id.ll_content_part,fragment);
transaction.commit();
}
private void chageButtonColor(int currentState,int toState){
switch (currentState){
case STATE_DIS:
this.btn_disc.setTextColor(Color.BLACK);
break;
case STATE_ME:
this.btn_me.setTextColor(Color.BLACK);
break;
case STATE_CON:
this.btn_contact.setTextColor(Color.BLACK);
break;
case STATE_MSG:
this.btn_msg_list.setTextColor(Color.BLACK);
break;
}
switch (toState){
case STATE_DIS:
this.btn_disc.setTextColor(Color.GREEN);
break;
case STATE_ME:
this.btn_me.setTextColor(Color.GREEN);
break;
case STATE_CON:
this.btn_contact.setTextColor(Color.GREEN);
break;
case STATE_MSG:
this.btn_msg_list.setTextColor(Color.GREEN);
break;
}
}
}
来源:https://blog.csdn.net/m0_46350041/article/details/107449879
标签:Android,Fragment,通知栏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#解析Lrc歌词文件过程详解
2021-12-26 05:52:38
![](https://img.aspxhome.com/file/2023/0/113890_0s.jpg)
C#byte数组与Image的相互转换实例代码
2023-08-15 16:15:51
java基于JDBC连接Oracle 11g Release2实例分析
2022-06-06 02:36:36
Android SurfaceView画板操作
2022-07-10 15:12:23
JAVA字符串占位符使用方法实例
2021-09-20 17:30:27
java搜索无向图中两点之间所有路径的算法
2023-11-10 09:28:26
springboot中filter的用法详解
2022-04-02 19:52:37
Java数据结构学习之树
2022-01-19 23:40:58
![](https://img.aspxhome.com/file/2023/8/122018_0s.png)
java虚拟机原理:Class字节码二进制文件分析
2022-02-06 09:02:33
![](https://img.aspxhome.com/file/2023/6/101946_0s.jpg)
Android 消息机制详解及实例代码
2023-07-28 08:37:49
![](https://img.aspxhome.com/file/2023/3/85963_0s.png)
SpringBoot上传文件大小受限问题的解决办法
2023-04-19 09:46:16
![](https://img.aspxhome.com/file/2023/9/63629_0s.jpg)
Android自定义实现开关按钮代码
2022-04-07 21:35:48
![](https://img.aspxhome.com/file/2023/7/138887_0s.png)
Spring之WEB模块配置详解
2023-12-05 13:21:48
java使用dom4j解析xml配置文件实现抽象工厂反射示例
2022-11-10 15:45:38
Android开发应用中Broadcast Receiver组件详解
2023-04-25 09:35:35
![](https://img.aspxhome.com/file/2023/1/122231_0s.png)
maven如何打包动态环境变量(包括启动脚本)
2023-07-16 03:48:56
SpringCloud Zuul过滤器和谷歌Gauva实现限流
2022-08-31 11:59:21
![](https://img.aspxhome.com/file/2023/5/125225_0s.png)
c#初学简单程序实例代码介绍
2023-11-04 10:53:53
浅析Java中线程的创建和启动
2022-12-29 17:37:41
![](https://img.aspxhome.com/file/2023/1/89871_0s.jpg)
Java学习之线程同步与线程间通信详解
2022-05-27 08:03:59
![](https://img.aspxhome.com/file/2023/6/106186_0s.png)