版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么在Android中使用PagerBottomTabStrip组件实现一个底部菜单功能
这篇文章将为大家详细讲解有关怎么在Android中使用PagerBottomTabStrip组件实现一个底部菜单功能,文章内容质量较高,因此在下分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。1、前言(1)底部选择菜单功能应该是大多app都会用到的,实现方式也有很多种,比较笨的方法可以自定义一个xml,下方布局样式,每次点击不同按钮时跳转到不同activity,这个activity重新加载一下底部菜单(2)今天介绍一个网上比较流行的底部菜单PagerBottomTabStrip功能,主要是这个菜单样式比价好看,而且点击时有点击效果,感觉还是不错的,而且也可以在菜单上加数字显示。功能算是比较全的吧。在GitHub上有2000多个star,所以选择它作为项目的底部菜单:/tyzlmjj/PagerBottomTabStrip。(3)当然还有一个框架也不错,可以参考:/ogaclejapan/SmartTabLayout(4)效果图:2、底部导航菜单功能代码1、首先需要引用包:compile
'me.majiajie:pager-bottom-tab-strip:2.2.5'2、然后写一个主的activity和底部点击进入的两个Fragment:class
MainBottomTabActivity
:
BaseActivity()
{
private
val
mFragments
=
ArrayList<Fragment>()
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])
mitAllowingStateLoss()
}
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])
mitAllowingStateLoss()
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的styleandroid:theme="@style/AppTheme"
<style
name="AppTheme"
parent="Theme.AppCompat.Light.NoActionBar">
<!--
Customize
your
theme
here.
-->
<item
name="android:windowBackground">@drawable/splash_bg</item>
<item
name="colorPrimary">@color/white</item>
<item
name="colorPrimaryDark">@color/black</item>
<item
name="colorAccent">@color/blue</item>
<item
name="actionBarSize">48dip</item>
<item
name="android:textColorPrimary">@color/black</item>
<item
name="toolbarNavigationButtonStyle">@style/myToolbarNavigationButtonStyle</item>
</style>(2)自定义顶部top.xml<?xml
version="1.0"
encoding="utf-8"?>
<LinearLayout
xmlns:android="/apk/res/android"
android:id="@+id/top_all"
android:layout_width="match_parent"
android:background="@color/white"
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/top_navigation"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:background="@color/grey"
android:gravity="center_vertical">
<!--上方导航条返回按钮-->
<LinearLayout
android:id="@+id/back_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:src="@drawable/back_btn"
android:layout_marginLeft="5dp"
android:layout_height="wrap_content"
/>
</LinearLayout>
<TextView
android:id="@+id/navication_text"
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="首页"
android:textColor="@color/font_title"
android:textSize="17dp"/>
<!--文字显示-->
<TextView
android:id="@+id/second_transfer_text"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="提交"
android:textColor="@color/blue"
android:visibility="invisible"
android:textSize="@dimen/text_size_14"
android:layout_height="match_parent"
/>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:background="@color/blue"
android:layout_height="@dimen/px_2"
/>
</LinearLayout>
(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
pu
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 特钢行业发展分析及投资价值研究咨询报告2024-2034版
- 中国集装袋行业发展趋势及发展前景研究报告2024-2034版
- 中国轮胎胶囊行业市场现状分析及竞争格局与投资发展研究报告2024-2034版
- 中国苯酚、丙酮行业市场现状分析及竞争格局与投资发展研究报告2024-2034版
- 中国生物书籍行业发展趋势及发展前景研究报告2024-2034版
- 中国游泳池过滤泵行业市场现状分析及竞争格局与投资发展研究报告2024-2034版
- 中国汽车用液力变矩器行业市场现状分析及竞争格局与投资发展研究报告2024-2034版
- 中国墨及碳素制品行业市场现状分析及竞争格局与投资发展研究报告2024-2034版
- 中国发电机组行业发展分析及投资风险预测分析报告2024-2034版
- 中国农机培训行业发展分析及发展前景与投资研究报告2024-2034版
- LY/T 2649-2016自然保护区生物多样性保护价值评估技术规程
- GB/T 6438-2007饲料中粗灰分的测定
- GB/T 37546-2019无人值守变电站监控系统技术规范
- 南京理工大学博士、硕士学位论文撰写格式
- 知识成果管理办法
- 英语备忘录写法课件
- 《骨科公休会》教学课件
- 研发中心建设方案
- SMW工法桩施工方案39099
- 中国慢性胃炎共识意见解读
- 公司项目精细化管理实施细则责任矩阵
评论
0/150
提交评论