TabLayout中的Tab.setCustomView左右有空隙,TabLayout下划线间隙设置,下划线长度设置
生活随笔
收集整理的這篇文章主要介紹了
TabLayout中的Tab.setCustomView左右有空隙,TabLayout下划线间隙设置,下划线长度设置
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
間隙設(shè)置為零?
因?yàn)楫?dāng)我們引入TabLayout時(shí)就已經(jīng)默認(rèn)tabPaddingStart為12dp,tabPaddingEnd為12dp.才會(huì)導(dǎo)致不能填滿(mǎn)的原因,這時(shí)我們只需要修改樣式或者屬性即可。
xmlns:app="http://schemas.android.com/apk/res-auto" app:tabPaddingStart="0dp" app:tabPaddingEnd="0dp"間隙大小設(shè)置,下劃線(xiàn)長(zhǎng)度設(shè)置?
<com.google.android.material.tabs.TabLayoutandroid:layout_below="@+id/topli11"android:id="@+id/activity_tablayoutt"android:layout_width="match_parent"android:layout_height="@dimen/normal_110dp"android:background="@color/white"app:tabBackground="@null"app:tabRippleColor="@null"app:tabIndicatorColor="#4065E0"app:tabIndicatorHeight="@dimen/normal_5dp"app:tabIndicatorFullWidth="false"app:tabPaddingStart="@dimen/normal_70dp"app:tabPaddingEnd="@dimen/normal_70dp"app:tabMaxWidth="@dimen/normal_305dp"/>?
<com.google.android.material.tabs.TabLayoutandroid:layout_below="@+id/topli11"android:id="@+id/activity_tablayoutt"android:layout_width="match_parent"android:layout_height="@dimen/normal_110dp"android:background="@color/white"app:tabBackground="@null"app:tabRippleColor="@null"app:tabIndicatorColor="#4065E0"app:tabIndicatorHeight="@dimen/normal_4dp"app:tabMaxWidth="@dimen/normal_180dp" />?
方法二——api28以下使用
tabLayout.post(new Runnable() {@Overridepublic void run() {setIndicator(tabLayout);} });?-----------------------------------------------------------------------------
/*** 設(shè)置tabLayout下劃線(xiàn)的寬*/ public static void setIndicator(TabLayout tabs) {Class<?> tabLayout = tabs.getClass();Field tabStrip = null;try {tabStrip = tabLayout.getDeclaredField("slidingTabIndicator");} catch (NoSuchFieldException e) {e.printStackTrace();}tabStrip.setAccessible(true);LinearLayout llTab = null;try {llTab = (LinearLayout) tabStrip.get(tabs);} catch (IllegalAccessException e) {e.printStackTrace();}//因?yàn)槲蚁胍男Ч? 字多寬線(xiàn)就多寬,所以測(cè)量mTextView的寬度f(wàn)or (int i = 0, count = llTab.getChildCount(); i < count; i++) {//獲取tabViewView tabView = llTab.getChildAt(i);//拿到tabView的mTextView屬性Field mTextViewField = null;try {//獲取tabView的textView屬性mTextViewField = tabView.getClass().getDeclaredField("textView");} catch (NoSuchFieldException e) {e.printStackTrace();}mTextViewField.setAccessible(true);TextView textView = null;try {textView = (TextView) mTextViewField.get(tabView);} catch (IllegalAccessException e) {e.printStackTrace();}tabView.setPadding(0, 0, 0, 0);//獲取textview寬度int textWidth = 0;textWidth = textView.getWidth();if (textWidth == 0) {textView.measure(0, 0);textWidth = textView.getMeasuredWidth();}//獲取tabview寬度int tabWidth = 0;tabWidth = tabView.getWidth();if (tabWidth == 0) {tabView.measure(0, 0);tabWidth = tabView.getMeasuredWidth();}//設(shè)置下劃線(xiàn)margin值LinearLayout.LayoutParams tabViewParams = (LinearLayout.LayoutParams) tabView.getLayoutParams();int margin = (tabWidth - textWidth) / 2;tabViewParams.leftMargin = margin;tabViewParams.rightMargin = margin;tabView.setLayoutParams(tabViewParams);}}總結(jié)
以上是生活随笔為你收集整理的TabLayout中的Tab.setCustomView左右有空隙,TabLayout下划线间隙设置,下划线长度设置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: JAVA spring 常用包作用详解(
- 下一篇: 屏幕控制实现消息发送以及轰炸