从网上搜到的实现:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//官方原版
TabLayout tabOfficial= (TabLayout) findViewById(R.id.tl_official);
addTabs(tabOfficial);
//反射
TabLayout tabReflex= (TabLayout) findViewById(R.id.tl_reflex);
addTabs(tabReflex);
//添加完所有tab后调用!!
reflex(tabReflex );
}
public void reflex(final TabLayout tabLayout){
//了解源码得知 线的宽度是根据 tabView的宽度来设置的
tabLayout.post(new Runnable() {
@Override
public void run() {
try {
//拿到tabLayout的mTabStrip属性
Field mTabStripField = tabLayout.getClass().getDeclaredField("mTabStrip");
mTabStripField.setAccessible(true);
LinearLayout mTabStrip = (LinearLayout) mTabStripField.get(tabLayout);
int dp10 = dip2px(tabLayout.getContext(), 10);
for (int i = 0; i < mTabStrip.getChildCount(); i++) {
View tabView = mTabStrip.getChildAt(i);
//拿到tabView的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 int dip2px(Context context, float dipValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
public void addTabs(TabLayout tabLayout){
tabLayout.addTab(tabLayout.newTab().setText("全部分类"));
tabLayout.addTab(tabLayout.newTab().setText("水果"));
tabLayout.addTab(tabLayout.newTab().setText("茶水饮料"));
tabLayout.addTab(tabLayout.newTab().setText("肉类"));
tabLayout.addTab(tabLayout.newTab().setText("有机蔬菜"));
tabLayout.addTab(tabLayout.newTab().setText("推荐"));
tabLayout.addTab(tabLayout.newTab().setText("膨化食品"));
}
}
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:textColor="#17c73d"
android:text="官方原版"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:textSize="15sp"
android:layout_height="wrap_content" />
<android.support.design.widget.TabLayout
android:id="@+id/tl_official"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
app:tabGravity="fill"
app:tabIndicatorColor="#17c73d"
app:tabIndicatorHeight="2dp"
app:tabMode="scrollable"
app:tabSelectedTextColor="#17c73d"
app:tabTextColor="#333333" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layout_height="40dp">
<TextView
android:layout_width="wrap_content"
android:textColor="#17c73d"
android:text="反射版本"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:textSize="15sp"
android:layout_height="wrap_content" />
<android.support.design.widget.TabLayout
android:id="@+id/tl_reflex"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
app:tabGravity="fill"
app:tabIndicatorColor="#17c73d"
app:tabIndicatorHeight="2dp"
app:tabMode="scrollable"
app:tabSelectedTextColor="#17c73d"
app:tabTextColor="#333333" />
</LinearLayout>
</LinearLayout>
应用到项目中的时候,反射方法获取mTabStrip属性的时候报错,查了一下原因是编译应用的时候把相应的类混淆了,修改了一下混淆文件,代码运行成功: