zoukankan      html  css  js  c++  java
  • 将Tab栏居中的方法

    原始tab:

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

    改变方法如下:

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

     1 private void populateTabStrip() {
     2         final PagerAdapter adapter = mViewPager.getAdapter();
     3         final View.OnClickListener tabClickListener = new TabClickListener();
     4 
     5         for (int i = 0; i < adapter.getCount(); i++) {
     6             View tabView = null;
     7             TextView tabTitleView = null;
     8 
     9             if (mTabViewLayoutId != 0) {
    10                 // If there is a custom tab view layout id set, try and inflate
    11                 // it
    12                 tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, false);
    13                 tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
    14             }
    15 
    16             if (tabView == null) {
    17                 tabView = createDefaultTabView(getContext());
    18             }
    19 
    20             if (tabTitleView == null && TextView.class.isInstance(tabView)) {
    21                 tabTitleView = (TextView) tabView;
    22             }
    23 
    24             tabTitleView.setText(adapter.getPageTitle(i));
    25             tabView.setOnClickListener(tabClickListener);
    26             
    27             mTabStrip.addView(tabView);
    28         }
    29     }

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

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

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

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

     1 private void populateTabStrip() {
     2         final PagerAdapter adapter = mViewPager.getAdapter();
     3         final View.OnClickListener tabClickListener = new TabClickListener();
     4 
     5         for (int i = 0; i < adapter.getCount(); i++) {
     6             View tabView = null;
     7             TextView tabTitleView = null;
     8 
     9             if (mTabViewLayoutId != 0) {
    10                 // If there is a custom tab view layout id set, try and inflate
    11                 // it
    12                 tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip, false);
    13                 tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
    14             }
    15 
    16             if (tabView == null) {
    17                 tabView = createDefaultTabView(getContext());
    18             }
    19 
    20             if (tabTitleView == null && TextView.class.isInstance(tabView)) {
    21                 tabTitleView = (TextView) tabView;
    22             }
    23 
    24             tabTitleView.setText(adapter.getPageTitle(i));
    25             tabView.setOnClickListener(tabClickListener);
    26 
    27             //添加
    28             LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(0, LayoutParams.WRAP_CONTENT, 1.0f);
    29             tabView.setLayoutParams(layoutParams);
    30             //添加
    31             
    32             mTabStrip.addView(tabView);
    33         }
    34     }
  • 相关阅读:
    大道至简伪代码形式读后感
    大道至简读后感
    使用类型转换生成六位验证字符,实现用户输入验证码的功能
    flex弹性布局的基本介绍
    清除浮动的方法以及为什么清除浮动
    :target伪类制作tab选项卡
    div中的内容水平垂直居中
    setTimeout和setInterval的区别以及如何写出效率高的倒计时
    margintop影响父元素定位
    关于页面中弹窗的定位问题
  • 原文地址:https://www.cnblogs.com/zzw1994/p/4987415.html
Copyright © 2011-2022 走看看