Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to set the width of indicator in TabLayout in Android

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report