Android api 28 tablayout変更
19051 ワード
tablayoutの下線の幅を設定します(TabLayoutと28の前に違いがあり、mTabStripはすでにslidingTabIndicatorで、API 28の下でTabLayoutの違い)
2つの実装方式
元簡書博文,現在ここに移転した
2つの実装方式
/**
* tablayout
*/
public static void reflex(final TabLayout tabLayout){
// tabView
tabLayout.post(new Runnable() {
@Override
public void run() {
try {
// tabLayout mTabStrip
LinearLayout mTabStrip = (LinearLayout) tabLayout.getChildAt(0);
int dp10 = dp2px(50);
for (int i = 0; i < mTabStrip.getChildCount(); i++) {
View tabView = mTabStrip.getChildAt(i);
// tabView mTextView tab mTextView
Field mTextViewField = tabView.getClass().getDeclaredField("mTextView");
mTextViewField.setAccessible(true);
TextView mTextView = (TextView) mTextViewField.get(tabView);
tabView.setPadding(0, 0, 0, 0);
// , mTextView
int width = 0;
width = mTextView.getWidth();
if (width == 0) {
mTextView.measure(0, 0);
width = mTextView.getMeasuredWidth();
}
// tab 10dp Padding tabView
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
params.width = width ;
params.leftMargin = dp10;
params.rightMargin = dp10;
tabView.setLayoutParams(params);
tabView.invalidate();
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
});
}
public static void setIndicatorWidth(@NonNull final TabLayout tabLayout, final int margin) {
tabLayout.post(new Runnable() {
@Override public void run() {
try {
// tabLayout slidingTabIndicator
Field slidingTabIndicatorField = tabLayout.getClass().getDeclaredField("slidingTabIndicator");
slidingTabIndicatorField.setAccessible(true);
LinearLayout mTabStrip = (LinearLayout) slidingTabIndicatorField.get(tabLayout);
for (int i = 0; i < mTabStrip.getChildCount(); i++) { View tabView = mTabStrip.getChildAt(i);
// tabView mTextView
Field textViewField = tabView.getClass().getDeclaredField("textView");
textViewField.setAccessible(true);
TextView mTextView = (TextView) textViewField.get(tabView);
tabView.setPadding(0, 0, 0, 0);
// , mTextView
int width = mTextView.getWidth();
if (width == 0) { mTextView.measure(0, 0);
width = mTextView.getMeasuredWidth();
}
// tab , Padding, tabView
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) tabView.getLayoutParams();
params.width = width; params.leftMargin = margin; params.rightMargin = margin;
tabView.setLayoutParams(params); tabView.invalidate();
}
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
});
}
元簡書博文,現在ここに移転した