【Android】导航栏(加图片icon)和不同页面的实现(viewpager+tablayout)
先上图,然后说大致步骤,最后再说细节

图片效果:依序点击导航栏左一、左二、中、右二、右一,最后直接滑动页面(不依靠导航栏切换)
大致步骤如下(文末会有完整代码)
【1】创建一个类,我这里取名TabBarViewPager,然后继承ViewPager
【2】在TabBarViewPager类里面,添加构造函数、onTouchEvent(可以使滑动页面失效,只能使用下方的导航栏)
【3】在需要用到的类(本文的类是TabBar.java),添加相关会用到的对象
【4】在TabBar里面添加内部类ViewPagerAdapter,继承自PagerAdapter,往里面添加构造函数,并且复写5个方法
【5】添加函数initTabLayoutView(方便管理)
【6】在onCreate里面用图片数量填充tabImg
【7】填充后,执行initTabLayoutView
【8】layout布置、style内容
大致步骤的细节如下
【细节1、2】创建类,如果规定用户只能使用导航栏切换,在onTouchEvent里面把第一行注释,下面的两行注释拿掉
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent; public class TabBarViewPager extends ViewPager { // 创建一个类,继承ViewPager
// 构造函数
public TabBarViewPager(@NonNull Context context) {
super(context);
} // 构造函数
public TabBarViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
} @Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return super.onInterceptTouchEvent(ev);
} @Override
public boolean onTouchEvent(MotionEvent ev) { // onTouchEvent
return super.onTouchEvent(ev);
//requestDisallowInterceptTouchEvent(true); // 屏蔽触摸事件
//return false; // 禁止页面滑动事件
}
}
【细节3】添加对象
TabBarViewPager mViewPager // 刚刚自己创建的类,实现页面切换 TabLayout mTabLayout // 导航栏需要 LayoutInflater mInflater // 页面绑定xml需要 View view1,view2,view3,view4,view5 // 添加页面需要 List<View> mViewList = new ArrayList<>() // 所有页面的集合 int[] tabImg // 导航栏图片需要,这里没给数量,等等在onCreate里面用图片数量来决定
【细节4】写内部类
class ViewPagerAdapter extends PagerAdapter {
private List<View> mViewList;
private Context context;
private int[] tabImg;
private ViewPagerAdapter(Context context, List<View> mViewList, int[] tabImg) { // 构造函数
this.mViewList = mViewList;
this.context = context;
this.tabImg = tabImg;
}
@Override
public int getCount() {
return mViewList.size();
}
@Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
}
@NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
container.addView(mViewList.get(position));
return mViewList.get(position);
}
@Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(mViewList.get(position));
}
@Nullable
@Override
public CharSequence getPageTitle(int position) { // 导航栏的图片
Drawable dImage = context.getResources().getDrawable(tabImg[position]);
dImage.setBounds(0, 0, dImage.getIntrinsicWidth(), dImage.getIntrinsicHeight());
SpannableString sp = new SpannableString(" "); // 这里要给图片留一个字符的空间
ImageSpan imageSpan = new ImageSpan(dImage, ImageSpan.ALIGN_BOTTOM);
sp.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sp;
}
}
【细节5】添加init函数
void initTabLayoutView() {
mViewPager = findViewById(R.id.vp_view);
mTabLayout = findViewById(R.id.tabs);
mInflater = LayoutInflater.from(this);
view1 = mInflater.inflate(R.layout.layout_view1, null);
view2 = mInflater.inflate(R.layout.layout_view2, null);
view3 = mInflater.inflate(R.layout.layout_view3, null);
view4 = mInflater.inflate(R.layout.layout_view4, null);
view5 = mInflater.inflate(R.layout.layout_view5, null);
mViewList.add(view1);
mViewList.add(view2);
mViewList.add(view3);
mViewList.add(view4);
mViewList.add(view5);
mTabLayout.setTabMode(TabLayout.MODE_FIXED); // 设置tab模式为系统默认模式
ViewPagerAdapter mVPAdapter = new ViewPagerAdapter(this, mViewList, tabImg);
mViewPager.setAdapter(mVPAdapter);
mTabLayout.setupWithViewPager(mViewPager);
}
【细节6、7】在onCreate里面添加最后代码
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide(); // 隐藏标题栏
setContentView(R.layout.cell_tabbar_view); tabImg = new int[] {
R.drawable.tab_icon1,
R.drawable.tab_icon2,
R.drawable.tab_icon3,
R.drawable.tab_icon4,
R.drawable.tab_icon5
};
initTabLayoutView(); // 初始化“页面”和“导航栏”
}
【细节8】onCreate里面的cell_tabbar_view(layout布置)、style内容
其中
【xxxxx】是你自己的项目名称
【TabBarViewPager】是刚刚创建过的类
【android.support.design.widget.TabLayout】是依赖包的东西,添加的方法:https://www.cnblogs.com/PureHeart/p/10342247.html
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"> <com.example.xxxxx.TabBarViewPager
android:id="@+id/vp_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="11"/> <android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#cccccc"
app:tabGravity="fill"
app:tabIndicatorHeight="0dp"
app:tabMode="fixed"
app:tabTextAppearance="@style/styleTabBarLayout"/> </LinearLayout>
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
<style name="styleTabBarLayout" parent="TextAppearance.Design.Tab">
<item name="textAllCaps">false</item>
<item name="android:textAllCaps">false</item>
<!-- 这两个属性一定要有,要不然图片不会显示 -->
</style>
</resources>
完整代码
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; import java.util.ArrayList;
import java.util.List; public class TabBar extends AppCompatActivity { private TabBarViewPager mViewPager;
private TabLayout mTabLayout;
private LayoutInflater mInflater;
private View view1,view2,view3,view4,view5;
private List<View> mViewList = new ArrayList<>();
private int[] tabImg; void initTabLayoutView() {
mViewPager = findViewById(R.id.vp_view);
mTabLayout = findViewById(R.id.tabs);
mInflater = LayoutInflater.from(this);
view1 = mInflater.inflate(R.layout.layout_view1, null);
view2 = mInflater.inflate(R.layout.layout_view2, null);
view3 = mInflater.inflate(R.layout.layout_view3, null);
view4 = mInflater.inflate(R.layout.layout_view4, null);
view5 = mInflater.inflate(R.layout.layout_view5, null); mViewList.add(view1);
mViewList.add(view2);
mViewList.add(view3);
mViewList.add(view4);
mViewList.add(view5); mTabLayout.setTabMode(TabLayout.MODE_FIXED); // 设置tab模式为系统默认模式 ViewPagerAdapter mVPAdapter = new ViewPagerAdapter(this, mViewList, tabImg);
mViewPager.setAdapter(mVPAdapter);
mTabLayout.setupWithViewPager(mViewPager);
} class ViewPagerAdapter extends PagerAdapter {
private List<View> mViewList;
private Context context;
private int[] tabImg; private ViewPagerAdapter(Context context, List<View> mViewList, int[] tabImg) {
this.mViewList = mViewList;
this.context = context;
this.tabImg = tabImg;
} @Override
public int getCount() {
return mViewList.size();
} @Override
public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
return view == object;
} @NonNull
@Override
public Object instantiateItem(@NonNull ViewGroup container, int position) {
container.addView(mViewList.get(position));
return mViewList.get(position);
} @Override
public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
container.removeView(mViewList.get(position));
} @Nullable
@Override
public CharSequence getPageTitle(int position) {
Drawable dImage = context.getResources().getDrawable(tabImg[position]);
dImage.setBounds(0, 0, dImage.getIntrinsicWidth(), dImage.getIntrinsicHeight()); SpannableString sp = new SpannableString(" "); // 这里要给图片留一个字符的空间
ImageSpan imageSpan = new ImageSpan(dImage, ImageSpan.ALIGN_BOTTOM);
sp.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sp;
}
} @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide(); // 隐藏标题栏
setContentView(R.layout.cell_tabbar_view); tabImg = new int[] {
R.drawable.tab_icon1,
R.drawable.tab_icon2,
R.drawable.tab_icon3,
R.drawable.tab_icon4,
R.drawable.tab_icon5
};
initTabLayoutView(); // 初始化“页面”和“导航栏”
}
}
【Android】导航栏(加图片icon)和不同页面的实现(viewpager+tablayout)的更多相关文章
- 微信小程序 自定义头部导航栏和导航栏背景图片 navigationStyle
这两天因为要做一个带背景的小程序头,哭了,小程序导航栏有背景也就算了,还得让导航栏上的背景顺下来,心态小崩.现在可以单独设置一个页面的小程序头了,但是前提是要微信7.0以上的版本,考虑到兼容性问题 ...
- Swift 导航栏设置图片点击事件,图片蓝色的解决方案
如果导航栏想做一个点击事件,正好是一个图片 我们可以直接这样: self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: UIIm ...
- ios怎么让状态栏颜色和导航栏背景图片颜色一样
ios7 图片作为导航的背景的话,如果想实现状态栏和导航栏一体化,那么图片高度需要增加22,也就是64,retina是128
- react-native react-navigation StackNavigator android导航栏 标题下居中
navigationOptions:({ navigation }) => ({ , textAlign:'center' }}) 如上设置即可,如果有返回箭头,那么右边也要加一个 占位的或者设 ...
- unity 显示、隐藏Android导航栏
1.下面的返回.home栏可用Screen.fullScreen控制 2.导航栏的显示和隐藏用下面代码控制 private AndroidJavaObject currentActivity { ge ...
- 使用webpack从0搭建多入口网站脚手架,可复用导航栏/底部通栏/侧边栏,根据页面文件自动更改配置,支持ES6/Less
之前只知道webpack很强大,但是一直没有深入学习过,这次从头看了一下教程,然后从0开始搭建了一个多入口网站的开发脚手架,期间遇到过很多问题,所以有心整理一下,希望能给大家一点帮助. 多HTML网站 ...
- Android导航栏ActionBar的具体分析
尊重原创:http://blog.csdn.net/yuanzeyao/article/details/39378825 关于ActionBar,相信大家并不陌生,可是真正能够熟练使用的也不是许多,这 ...
- Android导航栏菜单强制转换
private void getOverflowMenu() { ViewConfiguration viewConfig = ViewConfiguration.get(this); try { F ...
- [转]layui点击左侧导航栏,实现不刷新整个页面,只刷新局部
本文转自:https://blog.csdn.net/s31415926_004/article/details/84256587 其实这篇文章是给自己看的,以后忘记怎么做回来还能看一下哈哈哈哈哈哈哈 ...
随机推荐
- SQL Server ->> 自动创建表并从文件加载数据
这个存储过程自动创建表并从文件加载数据. 有一点需要说明的是Excel 12.0驱动是兼容了Excel 97-2003和Excel 2007两者格式的Excel文件. CREATE PROCEDURE ...
- 测试mysql性能工具
mysqlslap mysqlslap可以模拟服务器的负载,并输出计时信息.它包含在MySQL 5.1 的发行包中,应该在MySQL 4.1或者更新的版本中都可以使用.测试时可以执行并发连接数,并指定 ...
- hexo安装-nodejs
npm- 安装淘宝源 http://npm.taobao.org/$ npm install -g cnpm --registry=https://registry.npm.taobao.org- 查 ...
- Spring学习之-各注解的含义总结
注解配置 @ComponentScan("spittr.web"):/在加载Spring上下文时,会扫描spittr.web包查找组件 @ComponentScan注解扫描的组件有 ...
- February 19 2017 Week 8 Sunday
We accept the love we think we deserve. 我们接受自己认为配得上的爱. A few months ago, I tried to date with a girl ...
- Go语言(三)反射机制
package main import( "fmt" "reflect" ) func main(){ // iterate through the attri ...
- 罗大佑 光阴的故事 ZT 欧美经典歌曲100首(1-50)
老俞 my idol ———————————————————————————————— 罗大佑 光阴的故事 歌曲光阴的故事为2008年入库,是罗大佑在2000-1-1发行的专辑<情歌精选> ...
- Entity Framework: 主从表的增删改
1.根据主表添加从表数据 var dest = (from d in context.Destinations where d.Name == "Bali" select d).S ...
- FireFox浏览器Flash&视频下载工具推荐
介绍 两款扩展组件:Flash and Video Download & Flash Video Downloader 一起使用,各有优缺点. Flash and Video Download ...
- 转:Recsys2013论文导读
月中在香港参加recsys2013会议,文章不少,对我有价值的并不算多,再跟目前工作相关的就更少了.这里过滤了几篇我觉得比较有意思的文章,加上了自己的理解,作为导读. A Fast Parallel ...