当前位置:网站首页>Tablayout set spacing

Tablayout set spacing

2020-12-06 08:51:54 osc_ eqcu0796

Tablayout Set up item spacing

  • Method :1
/**
   *  Set up tablayout The underline changes according to the width of the text 
   * @param tabLayout
   * @param padding
   */
  public void setTabWidth(final TabLayout tabLayout, final int padding){
    tabLayout.post(new Runnable() {
      @Override
      public void run() {
        try {
          // Get tabLayout Of mTabStrip attribute 
          LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);

          for (int i = 0; i < mTabStrip.getChildCount(); i++) {
            View tabView = mTabStrip.getChildAt(i);

            // Get tabView Of mTextView attribute   tab The number of words is not fixed mTextView
            Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
            mTextViewField.setAccessible(true);

            TextView mTextView = (TextView) mTextViewField.get(tabView);
            tabView.setPadding(0, 0, 0, 0);
            // As wide a word as a line is , So measure mTextView Width 
            int width = 0;
            width = mTextView.getWidth();
            if (width == 0) {
              mTextView.measure(0, 0);
              width = mTextView.getMeasuredWidth();
            }
            // Set up tab Left and right space   Be careful not to use Padding  Because the width of the source line is based on  tabView To set the width of 
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
            params.width = width ;
            params.leftMargin = padding;
            params.rightMargin = padding;
            tabView.setLayoutParams(params);
            tabView.invalidate();
          }

        } catch (NoSuchFieldException e) {
          e.printStackTrace();
        } catch (IllegalAccessException e) {
          e.printStackTrace();
        }
      }
    });
  }
   setTabWidth(tab, 25);
    // This method is to be used in  tablayout  and viewPager  Before binding .tablayout  Property settings  app:tabMode="fixed"  It works 
      // binding viewPager  
    tab.setupWithViewPager(viewPager, false);

Add : If it's just for the same width as the font width ,tablayout New properties have been added

app:tabIndicatorFullWidth=“false”
The above method can set a fixed width

  • Method :2

stay Android Of TabLayout in item The spacing is the default , Before looking for a lot of methods, did not find the adjustment method , If you want to modify, you can give TabLayout Add the following two properties

app:tabMaxWidth=""
app:tabMinWidth="
``
 After adding these two attributes item The default spacing between them will disappear , You can give it to yourself at this time item Set the spacing , Still TabLayout Add the following properties to 

app:tabPaddingStart=""
app:tabPaddingEnd=""

In this way, we can achieve the goal of TabItem The spacing of has been changed
Another reference article is a good note :https://blog.csdn.net/u010845516/article/details/79563561?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-2.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-2.control

版权声明
本文为[osc_ eqcu0796]所创,转载请带上原文链接,感谢
https://chowdera.com/2020/12/202012060851321909.html