android -- the underline width of the higher version of Tablayout

Keywords: Mobile Android

Previously, there was a blog that wrote TabLayout, and recently developed and used the high-level version. I encountered some problems to summarize

Android--------TabLayout realizes the top navigation bar of news client

Tablayout in Android sets the underline width and the conversion between dp and px

 

There is no problem before API 28. After API 28, some places have changed

public static void reflex(final TabLayout tabLayout){
        tabLayout.post(() -> {
            try {
                //Get the slidingTabIndicator property of tabLayout
                Field tabIndicator = tabLayout.getClass().getDeclaredField("slidingTabIndicator");
                //mTabStrip below API28
//              Field tabIndicator = tabLayout.getClass().getDeclaredField("mTabStrip");
                tabIndicator.setAccessible(true);
                LinearLayout mTabStrip = (LinearLayout) tabIndicator.get(tabLayout);
                int dp10 = dip2px(tabLayout.getContext(), 10);

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

                    //If you get the mTextView property tab of tabView, the number of words is not fixed, you must use reflection to get mTextView
                    Field mTextViewField = tabView.getClass().getDeclaredField("textView");
                    //mTextView below API28
//                  Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
                    mTextViewField.setAccessible(true);
                    TextView mTextView = (TextView) mTextViewField.get(tabView);
                    tabView.setPadding(0, 0, 0, 0);

                    //As wide as a word, as wide as a line, you need to measure the width of mTextView
                    int width = 0;
                    width = mTextView.getWidth();
                    if (width == 0) {
                        mTextView.measure(0, 0);
                        width = mTextView.getMeasuredWidth();
                    }

                    //Set the left and right spacing of tab to 10dp, which can be changed according to your own needs
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
                    params.width = width ;
                    params.leftMargin = dp10;
                    params.rightMargin = dp10;
                    tabView.setLayoutParams(params);
                    tabView.invalidate();
                }

            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        });

    }

 

Of course, I don't really recommend this way. I also saw some netizens on the Internet saying that the setting didn't work

 

So after I used Android x, I found that the Tablayout in Android is a little different from the previous one, and it can also realize the underline problem

 

See blog

Posted by AliasXNeo on Sat, 02 Nov 2019 07:27:07 -0700