间隙设置为零
因为当我们引入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.TabLayout
android: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.TabLayout
android: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() {
@Override
public 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++) {
//获取tabView
View 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);
}
}
1844

被折叠的 条评论
为什么被折叠?



