TabLayout中的Tab.setCustomView左右有空隙,TabLayout下划线间隙设置,下划线长度设置
生活随笔
收集整理的這篇文章主要介紹了
TabLayout中的Tab.setCustomView左右有空隙,TabLayout下划线间隙设置,下划线长度设置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
間隙設置為零?
因為當我們引入TabLayout時就已經默認tabPaddingStart為12dp,tabPaddingEnd為12dp.才會導致不能填滿的原因,這時我們只需要修改樣式或者屬性即可。
xmlns:app="http://schemas.android.com/apk/res-auto" app:tabPaddingStart="0dp" app:tabPaddingEnd="0dp"間隙大小設置,下劃線長度設置?
<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);} });?-----------------------------------------------------------------------------
/*** 設置tabLayout下劃線的寬*/ 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();}//因為我想要的效果是 字多寬線就多寬,所以測量mTextView的寬度for (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();}//設置下劃線margin值LinearLayout.LayoutParams tabViewParams = (LinearLayout.LayoutParams) tabView.getLayoutParams();int margin = (tabWidth - textWidth) / 2;tabViewParams.leftMargin = margin;tabViewParams.rightMargin = margin;tabView.setLayoutParams(tabViewParams);}}總結
以上是生活随笔為你收集整理的TabLayout中的Tab.setCustomView左右有空隙,TabLayout下划线间隙设置,下划线长度设置的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: JAVA spring 常用包作用详解(
- 下一篇: 屏幕控制实现消息发送以及轰炸