资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么在Android中使用PagerBottomTabStrip组件实现一个底部菜单功能

这篇文章将为大家详细讲解有关怎么在Android中使用PagerBottomTabStrip组件实现一个底部菜单功能,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

创新互联建站-专业网站定制、快速模板网站建设、高性价比海城网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式海城网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖海城地区。费用合理售后完善,十年实体公司更值得信赖。

1、前言

(1)底部选择菜单功能应该是大多app都会用到的,实现方式也有很多种,比较笨的方法可以自定义一个xml,下方布局样式,每次点击不同按钮时跳转到不同activity,这个activity重新加载一下底部菜单

(2)今天介绍一个网上比较流行的底部菜单PagerBottomTabStrip功能,主要是这个菜单样式比价好看,而且点击时有点击效果,感觉还是不错的,而且也可以在菜单上加数字显示。功能算是比较全的吧。在GitHub上有2000多个star,所以选择它作为项目的底部菜单:https://github.com/tyzlmjj/PagerBottomTabStrip。

(3)当然还有一个框架也不错,可以参考:https://github.com/ogaclejapan/SmartTabLayout

(4)效果图:

怎么在Android中使用PagerBottomTabStrip组件实现一个底部菜单功能

2、底部导航菜单功能代码

1、首先需要引用包:

compile 'me.majiajie:pager-bottom-tab-strip:2.2.5'

2、然后写一个主的activity和底部点击进入的两个Fragment:

class MainBottomTabActivity : BaseActivity() {
 private val mFragments = ArrayList()
 var number:Int=8
 override fun onCreate(savedInstanceState: Bundle?) {
  super.onCreate(savedInstanceState)
  setContentView(R.layout.main_bottom_tab)
  //初始化Fragment
  initFragment()
  //初始化底部Button
  initBottomTab()
 }
 /**
  * 初始化四个导航页面
  */
 fun initFragment(){
  mFragments!!.add(TabBar1Fragment())
  mFragments!!.add(TabBar2Fragment())
  mFragments!!.add(TabBar1Fragment())
  //默认选中第一个
  val transaction = supportFragmentManager.beginTransaction()
  transaction!!.add(R.id.frameLayout, mFragments[0])
  transaction.commitAllowingStateLoss()
 }
 fun initBottomTab(){
  //这里要特别注意,pager_bottom_tab.custom()这句话就是选择自己需要的样式
  val navigationController = pager_bottom_tab.custom()
    .addItem(newItem(R.drawable.ic_restore_gray_24dp, R.drawable.ic_restore_teal_24dp, "消息"))
    .addItem(newItem(R.drawable.ic_favorite_gray_24dp, R.drawable.ic_favorite_teal_24dp, "工作"))
    .addItem(newItem(R.drawable.ic_nearby_gray_24dp, R.drawable.ic_nearby_teal_24dp, "我的"))
    .build()
  //设置消息数
  navigationController.setMessageNumber(1, number)
  //设置显示小圆点
  navigationController.setHasMessage(0, true)
  //底部按钮的点击事件监听
  navigationController.addTabItemSelectedListener(object : OnTabItemSelectedListener {
   override fun onSelected(index: Int, old: Int) {
    val transaction = supportFragmentManager.beginTransaction()
    transaction.replace(R.id.frameLayout, mFragments[index])
    transaction.commitAllowingStateLoss()
    if(index==0){
     navigationController.setHasMessage(0, false)
    }else if(index==1){
     navigationController.setMessageNumber(1, --number)
    }
   }
   override fun onRepeat(index: Int) {}
  })
 }
 //创建一个Item
 private fun newItem(drawable: Int, checkedDrawable: Int, text: String): BaseTabItem {
  val normalItemView = NormalItemView(this)
  normalItemView.initialize(drawable, checkedDrawable, text)
  normalItemView.setTextDefaultColor(Color.GRAY)
  normalItemView.setTextCheckedColor(-0xff6978)
  return normalItemView
 }
}

3、顶部导航功能

(1)定义activity的style

android:theme="@style/AppTheme"

 
 @drawable/splash_bg
 @color/white
 @color/black
 @color/blue
 48dip
 @color/black
 @style/myToolbarNavigationButtonStyle

(2)自定义顶部top.xml



 
  
  
   
  
  
  
  
 
 

  (3)在BaseActivity中写方法

protected void setTitle(Object title,Boolean right,Object rightContent) {
 try {
  TextView titleText=findViewById(R.id.navication_text);
  titleText.setText((String)title);
  //显示右侧的文字按钮
  if(right){
   TextView rightText=findViewById(R.id.second_transfer_text);
   rightText.setVisibility(View.VISIBLE);
   rightText.setText((String)rightContent);
  }
 } catch (Exception e) {
  e.printStackTrace();
 }
}
/**
 * 设置点击左上角的返回事件.默认是finish界面
 */
protected void registerBack() {
 LinearLayout llLeft = findViewById(R.id.back_btn);
 llLeft.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View view) {
   BaseActivity.this.finish();
  }
 });
}

(4)继承BaseActivity,xml包含includetop.xml然后直接执行方法


setTitle("首頁",false,null)
registerBack()

关于怎么在Android中使用PagerBottomTabStrip组件实现一个底部菜单功能就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网站标题:怎么在Android中使用PagerBottomTabStrip组件实现一个底部菜单功能
本文链接:http://cdkjz.cn/article/gogigd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220