Android Fragment实现底部通知栏,供大家参考,具体内容如下

截图如下:

1. 第一步先要创建fragment(动态注册)

然后将两个勾选取消掉(还有一种是自己手动创建)

会自动生成相对应的layout布局,剩下的要根据自己的需求了

2.在Activity的布局里写好四个按钮

这里不是重点…

android:layout_width="match_parent"

android:layout_height="match_parent"

>

android:id="@+id/ll_content_part"

android:layout_width="match_parent"

android:layout_height="match_parent"

>

android:id="@+id/ll_function"

android:layout_alignParentBottom="true"

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="wrap_content">

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"/>

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"/>

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"/>

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"/>

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;

}

}

}

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

android 底部通知,Android Fragment实现底部通知栏相关推荐

  1. android后台通知,Android后台定时提醒功能实现

    前提:考虑到自己每次在敲代码或者打游戏的时候总是会不注意时间,一不留神就对着电脑连续3个小时以上,对眼睛的伤害还是挺大的,重度近视了可是会遗传给将来的孩子的呀,可能老婆都跟别人跑了. 于是,为了保护眼 ...

  2. android 常驻通知,android实现常驻通知栏遇到的问题及解决办法

    实现常驻通知栏时遇到的问题: 无论如何就是不显示通知,查看日志发现貌似报错了: 2020-06-28 14:11:34.923 6387-6387/xxx E/CrashReport: android ...

  3. android 蓝牙通知,android – 蓝牙低能耗通知

    我正在尝试从健康档案中读取温度值.根据official google ble devlopment page,当我尝试通知健康温度计特性读取温度时,我尝试使用writeDescriptor写一个描述符 ...

  4. android receiver 通知,android – 来自BroadcastReceiver的呼叫通知

    我有代码: public void AlarmStart() { Calendar cal = Calendar.getInstance(); cal.add(Calendar.MINUTE, 5); ...

  5. android系统 通知,Android中通知的使用-----Notification详解

    Notification -- 通知,是一种让你的应用程序在不使用Activity的情况下警示用户.它是看不见的程序组件(Broadcast Receiver,Service和不活跃的Activity ...

  6. android receiver 通知,android Notification不会触发BroadcastReceiver的onReceive

    有可能通知开始广播接收者吗? 我试过这个代码,但它没有工作. 通知是创建的,但是当我点击它没有任何反应. 注意:当我将notificationIntent更改为从MyBroadcastReceiver ...

  7. Android P通知

    In this tutorial, we'll be discussing the changes introduced in the Notification System and its UI w ...

  8. Android移动开发之【Android实战项目】DAY2-使用Fragment实现底部菜单栏

    由于TabActivity在Android4.0以后已经被完全弃用,那么我就不再浪费口水继续讲解它了,取而代之的是Fragment.Fragment是Android3.0新增的概念,Fragment翻 ...

  9. Android studio实现底部导航,Android 开发之BottomBar+ViewPager+Fragment实现炫酷的底部导航效果...

    BottomBar BottomBar是Github上的一个开源框架,因为从1.3.3开始不支持fragments了,要自己配置,弄了很久,不管是app的fragment还是V4 的程序总是总是闪退. ...

最新文章

  1. 来自高维的对抗 - 逆向TinyTool自制
  2. iphone怎么看wifi密码_无线网密码正确但是手机连接不上wifi怎么回事?
  3. [Android]对MVC和MVP的总结
  4. 2021年中国超声波织物切割机市场趋势报告、技术动态创新及2027年市场预测
  5. webcontent 与 webroot 问题解决
  6. UICollectionViewController
  7. 实体类转换为XML字符串
  8. 极域电子教室破解控制---万能密码、查找密码
  9. 视频加密软件技术小分享
  10. 怎么保存html,怎样保存网页?
  11. 天津大学软件学院 研究生学位申请的学术成果要求
  12. 理想国pandas练习题4
  13. 加密一条保序的数据流
  14. MySQL中添加新字段
  15. 悬浮细胞、淋巴细胞培养常见失败原因及解决方案
  16. python xlsx文件与csv文件转换
  17. 计算机考研分数403,总分403分过来人分享成功考研经验_跨考网
  18. 盖泽尔智力测试软件,宝宝聪明不聪明?只需测一测,不用去医院,家长在家轻松筛查...
  19. 朴素贝叶斯算法原理、代码实现原理、以及鸢尾花分类代码实现(详细代码原理讲解)
  20. 常见java本地缓存

热门文章

  1. python set union_python – set.union()抱怨它在传入生成器时没有参数
  2. mysql服务器性能剖析,高性能MySQL–服务器性能剖析
  3. Python中列表推导式同filter和map的比较
  4. python中os.path.isdir()等函数的作用及用法
  5. Python内置函数zip map filter的使用
  6. python教程:循环(while和for)
  7. Python中enumerate用法详解
  8. 相关与卷积(数字信号处理)的数学原理及 Python 实现
  9. c mysql 内存泄露_c代码连接mysql数据库内存泄露的问题
  10. Python flask出现jinja2.exceptions.TemplateNotFound错误(修复host就好了???)