|
|
@@ -1704,7 +1704,7 @@ public class CommonUtil {
|
|
|
* @param tabLayout
|
|
|
*/
|
|
|
public void reflex(final TabLayout tabLayout, final Context context) {
|
|
|
- reflex(tabLayout, context, false);
|
|
|
+ reflex(tabLayout, context, false, 0);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -1712,60 +1712,69 @@ public class CommonUtil {
|
|
|
*
|
|
|
* @param tabLayout
|
|
|
*/
|
|
|
- public void reflex(final TabLayout tabLayout, final Context context, final boolean reMeasure) {
|
|
|
+ public void reflex(final TabLayout tabLayout, final Context context, boolean reMeasure) {
|
|
|
+ reflex(tabLayout, context, reMeasure, 0);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过反射调整tabLayout中下划线的宽度
|
|
|
+ *
|
|
|
+ * @param tabLayout
|
|
|
+ */
|
|
|
+ public void reflex(final TabLayout tabLayout, final Context context, final boolean reMeasure, int tw) {
|
|
|
//了解源码得知 线的宽度是根据 tabView的宽度来设置的
|
|
|
- tabLayout.post(new Runnable() {
|
|
|
- @Override
|
|
|
- public void run() {
|
|
|
- try {
|
|
|
- //拿到tabLayout的mTabStrip属性
|
|
|
- LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);
|
|
|
+ tabLayout.post(() -> {
|
|
|
+ try {
|
|
|
+ //拿到tabLayout的mTabStrip属性
|
|
|
+ LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);
|
|
|
|
|
|
// int dp10 = dip2px(tabLayout.getContext(), 10);
|
|
|
- int add = context.getResources().getDimensionPixelSize(R.dimen.content_padding_10);
|
|
|
- int count = mTabStrip.getChildCount();
|
|
|
- int fillCount = Math.min(5, count);//一屏的个数
|
|
|
+ int add = context.getResources().getDimensionPixelSize(R.dimen.content_padding_10);
|
|
|
+ int count = mTabStrip.getChildCount();
|
|
|
+ int fillCount = Math.min(5, count);//一屏的个数
|
|
|
|
|
|
- int tabWidth = tabLayout.getWidth();
|
|
|
- if (tabWidth == 0) {
|
|
|
- tabWidth = G.WIDTH;
|
|
|
- }
|
|
|
- for (int i = 0; i < count; i++) {
|
|
|
- View tabView = mTabStrip.getChildAt(i);
|
|
|
+ int tabWidth = tw;
|
|
|
+ if (tabWidth == 0) {
|
|
|
+ tabWidth = tabLayout.getWidth();
|
|
|
+ }
|
|
|
+ if (tabWidth == 0) {
|
|
|
+ tabWidth = G.WIDTH;
|
|
|
+ }
|
|
|
+ for (int i = 0; i < count; i++) {
|
|
|
+ View tabView = mTabStrip.getChildAt(i);
|
|
|
|
|
|
- //拿到tabView的mTextView属性 tab的字数不固定一定用反射取mTextView
|
|
|
- Field mTextViewField = tabView.getClass().getDeclaredField("textView");//com.android.support:design:28.0.0
|
|
|
+ //拿到tabView的mTextView属性 tab的字数不固定一定用反射取mTextView
|
|
|
+ Field mTextViewField = tabView.getClass().getDeclaredField("textView");//com.android.support:design:28.0.0
|
|
|
// Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");//com.android.support:design:27.1.0
|
|
|
- mTextViewField.setAccessible(true);
|
|
|
-
|
|
|
- TextView mTextView = (TextView) mTextViewField.get(tabView);
|
|
|
-
|
|
|
- tabView.setPadding(0, 0, 0, 0);
|
|
|
+ mTextViewField.setAccessible(true);
|
|
|
|
|
|
- //因为我想要的效果是 字多宽线就多宽,所以测量mTextView的宽度
|
|
|
- int width = 0;
|
|
|
- width = mTextView.getWidth();
|
|
|
- if (width == 0 || reMeasure) {
|
|
|
- mTextView.measure(0, 0);
|
|
|
- width = mTextView.getMeasuredWidth();
|
|
|
- }
|
|
|
+ TextView mTextView = (TextView) mTextViewField.get(tabView);
|
|
|
|
|
|
- //设置tab左右间距为10dp 注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
|
|
|
- LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
|
|
|
- params.width = width;
|
|
|
- int margin = (tabWidth - fillCount * width - 2 * add) / (fillCount * 2);
|
|
|
- params.leftMargin = margin + (i == 0 ? add : 0);
|
|
|
- params.rightMargin = margin + (i + 1 == count ? add : 0);
|
|
|
- tabView.setLayoutParams(params);
|
|
|
+ tabView.setPadding(0, 0, 0, 0);
|
|
|
|
|
|
- tabView.invalidate();
|
|
|
+ //因为我想要的效果是 字多宽线就多宽,所以测量mTextView的宽度
|
|
|
+ int width = 0;
|
|
|
+ width = mTextView.getWidth();
|
|
|
+ if (width == 0 || reMeasure) {
|
|
|
+ mTextView.measure(0, 0);
|
|
|
+ width = mTextView.getMeasuredWidth();
|
|
|
}
|
|
|
|
|
|
- } catch (NoSuchFieldException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } catch (IllegalAccessException e) {
|
|
|
- e.printStackTrace();
|
|
|
+ //设置tab左右间距为10dp 注意这里不能使用Padding 因为源码中线的宽度是根据 tabView的宽度来设置的
|
|
|
+ LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
|
|
|
+ params.width = width;
|
|
|
+ int margin = (tabWidth - fillCount * width - 2 * add) / (fillCount * 2);
|
|
|
+ params.leftMargin = margin + (i == 0 ? add : 0);
|
|
|
+ params.rightMargin = margin + (i + 1 == count ? add : 0);
|
|
|
+ tabView.setLayoutParams(params);
|
|
|
+
|
|
|
+ tabView.invalidate();
|
|
|
}
|
|
|
+
|
|
|
+ } catch (NoSuchFieldException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ } catch (IllegalAccessException e) {
|
|
|
+ e.printStackTrace();
|
|
|
}
|
|
|
});
|
|
|
|