我知道这个问题是 2 年前提出的,但我没有找到任何简单的解决方案而不使用任何库(smartTabLayout 没有 SelectedTextColour 属性)。
反转你的 tabLayout 以获得顶部的指示器
android:rotationX="180"
现在这将导致该选项卡中的文本也被反转,因此可以解决这个问题
我们必须创建自定义选项卡视图。制作一个xml文件,例如:layout_custom_tab
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tab.text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="1"
android:padding="10dp"
android:rotationX="180"
android:textAllCaps="true"
android:textSize="12sp"
android:textColor="@color/white"
/>
注意:当只有一个元素时,你不需要 RelativeLayout 或其他任何东西
创建自己的 TabLayout
并将 customView 设置为它。
public class TabLayoutPlus extends TabLayout {
public TabLayoutPlus(Context context) {
super(context);
}
public TabLayoutPlus(Context context, AttributeSet attrs) {
super(context, attrs);
}
public TabLayoutPlus(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setupWithViewPager(ViewPager viewPager) {
super.setupWithViewPager(viewPager);
this.removeAllTabs();
PagerAdapter adapter = viewPager.getAdapter();
for (int i = 0, count = adapter.getCount(); i < count; i++) {
Tab tab = this.newTab();
View customView = LayoutInflater.from(getContext()).inflate(R.layout.layout_custom_tab, null);
TextViewPlus tv = (TextViewPlus) customView.findViewById(R.id.tab_text);
tv.setText(adapter.getPageTitle(i));
tab.setCustomView(customView);
this.addTab(tab.setText(adapter.getPageTitle(i)));
}
}
}
您的活动中的 TabLayout 将如下所示
<TabLayoutPlus
android:id="@+id/tablayout"
android:layout_width="match_parent"
android:layout_height="36dp"
android:layout_above="@+id/camera.buttons.layout"
android:layout_centerHorizontal="true"
android:background="@color/cardscan.background"
android:rotationX="180"
app:tabGravity="center"
app:tabIndicatorColor="@color/colorPrimary"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorPrimary"
app:tabTextColor="@color/white"
/>
如果您需要突出显示选定的选项卡文本颜色
private void setSelectedTab(TabLayoutPlus.Tab tab) {
View view = tab.getCustomView();
TextViewPlus tabtext = (TextViewPlus) view.findViewById(R.id.tab_text);
tabtext.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary)); // color you want to highlight your text with
}
private void setUnselectedTab(TabLayoutPlus.Tab tab){
View view = tab.getCustomView();
TextViewPlus tabtext = (TextViewPlus) view.findViewById(R.id.tab_text);
tabtext.setTextColor(ContextCompat.getColor(this, R.color.white)); // color you want to lowlight your text with
}
现在只需添加 OnTabSelectedListener
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
setSelectedTab(tab);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
setUnselectedTab(tab);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
setSelectedTab(tab);
}
});