Get the App
SLTechnology News&Howtos  ›  Development  › 

How to set the width of indicator in TabLayout in Android

Shulou Source: shulou.com Published: 2022-05-31 21:28:35 10月05日 Update

In this article Xiaobian for you to introduce in detail "how to set the width of the indicator in Android", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to set the width of the indicator in Android" can help you solve your doubts.

TabLayout is referenced in the anroid 5.0Design v7 package to simply and quickly write out your own Tab switching effect as shown in the figure:

But in normal use you find that you cannot set the width of the tablayout indicator. If you look at the source code, you will find that the designer sets the width of the indicator to the maximum width of TabView. And the designer did not expose the interface to us, which sometimes led to the use of TabLayout can not meet some product design requirements, such a good component can not be used, it takes time and effort to customize. At this time, we can get the indicator object in TabLayout through the reflection mechanism to deal with its width to meet our requirements: the specific code is as follows

Override the onMeasure method

@ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure (widthMeasureSpec, heightMeasureSpec); int dp10 = CommUitls.dip2px (context, 10); LinearLayout mTabStrip = (LinearLayout) this.getChildAt (0); try {Field mTabs = TabLayout.class.getDeclaredField ("mTabs"); mTabs.setAccessible (true); ArrayList tabs = (ArrayList) mTabs.get (this); for (int I = 0; I < mTabStrip.getChildCount (); iTunes +) {Tab tab = tabs.get (I) Field mView = tab.getClass (). GetDeclaredField ("mView"); mView.setAccessible (true); Object tabView = mView.get (tab); Field mTextView = context.getClassLoader (). LoadClass ("android.support.design.widget.TabLayout$TabView"). GetDeclaredField ("mTextView"); mTextView.setAccessible (true); TextView textView = (TextView) mTextView.get (tabView); float textWidth = textView.getPaint (). MeasureText (textView.getText (). ToString ()) View child = mTabStrip.getChildAt (I); child.setPadding (0,0,0,0); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams ((int) textWidth, LinearLayout.LayoutParams.MATCH_PARENT); params.leftMargin = dp10; params.rightMargin = dp10; child.setLayoutParams (params); child.invalidate ();} catch (Exception e) {e.printStackTrace () }} here, the article "how to set the indicator width of TabLayout in Android" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, please follow the industry information channel.

Tags: Width indication indicator article design content designer processing proper maximum product product design code can pass object idea interface effect new knowledge method Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Redmi Shulou Technology MariaDB Xiaomi