我编写了不那么干净的代码,但它对我很有用。我在这里分享它,以帮助任何需要它的人:
更改TabPageIndicator.addTab中的代码:
private void addTab(int index, CharSequence text, int iconResId) {
final TabView tabView = new TabView(getContext());
tabView.mIndex = index;
tabView.setFocusable(true);
tabView.setOnClickListener(mTabClickListener);
tabView.setText(text);
/**
* My parts (CullyCross):
*/
Resources r = getResources();
DisplayMetrics metrics = r.getDisplayMetrics();
float screenWidth = metrics.widthPixels;
float width = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 100, metrics);
int margin = (int)(screenWidth - 2 * width) / 4;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int)width, MATCH_PARENT);
params.setMargins(margin, 0, margin, 0);
/***********************/
if (iconResId != 0) {
tabView.setCompoundDrawablesWithIntrinsicBounds(iconResId, 0, 0, 0);
}
/**
* original
* mTabLayout.addView(tabView, new LinearLayout.LayoutParams(0, MATCH_PARENT, 1));
*/
mTabLayout.addView(tabView, params);
}
字体越薄越好。