原始tab:

居中后的tab(边缘效果是截图的问题):

改变方法如下:

找到Android SlidingTabLayout源代码,在Android SlidingTabLayout源代码中有一个方法:
private void populateTabStrip();

 private void populateTabStrip() {
final PagerAdapter adapter = mViewPager.getAdapter();
final View.OnClickListener tabClickListener = new TabClickListener(); for (int i = 0; i < adapter.getCount(); i++) {
View tabView = null;
TextView tabTitleView = null; if (mTabViewLayoutId != 0) {
// If there is a custom tab view layout id set, try and inflate
// it
tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, false);
tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
} if (tabView == null) {
tabView = createDefaultTabView(getContext());
} if (tabTitleView == null && TextView.class.isInstance(tabView)) {
tabTitleView = (TextView) tabView;
} tabTitleView.setText(adapter.getPageTitle(i));
tabView.setOnClickListener(tabClickListener); mTabStrip.addView(tabView);
}
}

这是谷歌官方实现的Android SlidingTabLayout添加底部选项卡Tab的代码,如果为了实现前文所述的将Tab均分水平位置空间,则需要修改此方法,在此方法中添加如下代码:

 LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f);
tabView.setLayoutParams(layoutParams);

这两段代码将tab view水平均分放置。

最终将private void populateTabStrip()改进成这样:

 private void populateTabStrip() {
final PagerAdapter adapter = mViewPager.getAdapter();
final View.OnClickListener tabClickListener = new TabClickListener(); for (int i = 0; i < adapter.getCount(); i++) {
View tabView = null;
TextView tabTitleView = null; if (mTabViewLayoutId != 0) {
// If there is a custom tab view layout id set, try and inflate
// it
tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, false);
tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
} if (tabView == null) {
tabView = createDefaultTabView(getContext());
} if (tabTitleView == null && TextView.class.isInstance(tabView)) {
tabTitleView = (TextView) tabView;
} tabTitleView.setText(adapter.getPageTitle(i));
tabView.setOnClickListener(tabClickListener); //添加
LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f);
tabView.setLayoutParams(layoutParams);
//添加 mTabStrip.addView(tabView);
}
}

将Tab栏居中的方法的更多相关文章

  1. tab栏切换,内容为不断实时刷新数据的vue实现方法

    先说一下产品需求,就是有几个tab栏,每个tab栏对应的ajax请求不一样,内容区域一样,内容为实时刷新数据,每3s需要重新请求,返回的数据在内容区域展示,每点击一次tab栏需停止其他tab栏ajax ...

  2. 小程序tab栏可滑动,可点击居中demo

    效果图: 代码: <view class="container"> <!-- tab导航栏 --> <!-- scroll-left属性可以控制滚动条 ...

  3. 面向对象tab栏例子分析

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. JavaScript实现Tab栏切换

    本文最初发表于博客园,并在GitHub上持续更新前端的系列文章.欢迎在GitHub上关注我,一起入门和进阶前端. 以下是正文. 京东网页上,可以看到下面这种tab栏的切换: 我们把模型抽象出来,实现一 ...

  5. 面向对象版Tab栏切换

    <div class="wrapper" id="wrapper"> <ul class="tab" id="t ...

  6. 关于在bootstrap的tab栏中渲染echats图表,切换tab时echats不显示问题

    在开发过程中遇到这样个问题: 利用bootstrap中的tab栏,每当点击tab栏的导航时,echats仅仅只渲染第一个tab的内容,切换tab时,echats图表不显示. 其html代码为: < ...

  7. 切换tab栏echarts错位的问题

    在使用echarts的时候页面中有tab栏的时候经常遇到echarts错位的情况 解决方法一.在点击tab栏的时候进行页面中的echarts初始化 在多层tab栏存在的时候eachrts的容器布局是百 ...

  8. GNU Radio下QT GUI Tab Widget的使用方法

    期望显示出的效果: 即将要显示的图放在各自的标签页中. 整体框图: 具体设置: QT GUI Tab Widget的设置: 其中 ID改为自己想改的,这里我写的是display GUI Hint所代表 ...

  9. 小程序开发顶部TAB栏和侧边分类点击

    先上一个效果图: 根据这个效果图我来说内容. 首先是顶部tab栏 效果实现依靠的是一个组件scroll-view.这个组件很有意思,可以多层嵌套,当然它的属性也很多. 这里主要用的是scroll-x, ...

随机推荐

  1. 9. Palindrome Number

    /* Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers ...

  2. iOS 进入后台的处理

    当App进入后台时(按下Home键), App会被系统暂停, 所有的程序逻辑都会停止, App还是驻留内存中, 除非被用户强制退出, 或者被系统kill掉(为了保证正在前台运行的App有足够的内存, ...

  3. Hbase基础操作

    $HBASE_HOME/bin/hbase org.apache.hadoop.hbase.mapreduce.RowCounter 'tablename'

  4. NEERC 2013, Eastern subregional contest

    I. The old Padawan Time limit: 0.5 secondMemory limit: 64 MB Yoda: Use the Force. Yes. Now, the ston ...

  5. 从AlphaGo谈通用型人工智能设计

    最近赢了人机大战的AlphaGo火了,火得一塌糊涂,圈里圈外,是人都在谈AlphaGo.但是AlphaGo毕竟是为特定场景特定应用设计的特定型人工智能,和通用型人工智能还是有很大差别,离人工智能普及更 ...

  6. freeglut第一步

    #include <GL/freeglut.h> static void RenderSceneCB() { glClear(GL_COLOR_BUFFER_BIT); glutSwapB ...

  7. Android开发-API指南-进程与线程

    Processes and Threads 英文原文:http://developer.android.com/guide/components/processes-and-threads.html ...

  8. coffeeScript 语法总结

    CoffeeScript ---->安装node.js ---->安装coffeeScript 语句: 注意:没有分号,语句由新的一行结束:多条语句写到同一行时需要分号表示一条语句的结束( ...

  9. 百度编辑器Ueditor自动换行,添加<p>的问题

    百度编辑器Ueditor其实蛮好用的,后来使用了一段时间发现,每次打开后又保存,发现都会往内容的前后都增加一个空白的<p></p>.刚开始以后是百度编辑器的问题,找了很长时间也 ...

  10. 如何利用jq来实现复选框的全选,反选!

    $("document").ready(function(){ $("#btn1").click(function(){ $("[name='chec ...