use*_*368 22
我改变了上面建议的答案,它对我很有用,不需要额外的.xml文件,希望它会有所帮助.
for (int i = 0; i < tabLayout.getTabCount(); i++) {
TabLayout.Tab tab = tabLayout.getTabAt(i);
if (tab != null) {
TextView tabTextView = new TextView(this);
tab.setCustomView(tabTextView);
tabTextView.getLayoutParams().width = ViewGroup.LayoutParams.WRAP_CONTENT;
tabTextView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
tabTextView.setText(tab.getText());
// First tab is the selected tab, so if i==0 then set BOLD typeface
if (i == 0) {
tabTextView.setTypeface(null, Typeface.BOLD);
}
}
}
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
TextView text = (TextView) tab.getCustomView();
text.setTypeface(null, Typeface.BOLD);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
TextView text = (TextView) tab.getCustomView();
text.setTypeface(null, Typeface.NORMAL);
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
Run Code Online (Sandbox Code Playgroud)
如果使用默认的TabLayout(而不是customView),则可以使用getChildAt()方法获取Tab的TextView.
.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
LinearLayout tabLayout = (LinearLayout)((ViewGroup) mMainTabs.getChildAt(0)).getChildAt(tab.getPosition());
TextView tabTextView = (TextView) tabLayout.getChildAt(1);
tabTextView.setTypeface(tabTextView.getTypeface(), Typeface.BOLD);
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
LinearLayout tabLayout = (LinearLayout)((ViewGroup) mMainTabs.getChildAt(0)).getChildAt(tab.getPosition());
TextView tabTextView = (TextView) tabLayout.getChildAt(1);
tabTextView.setTypeface(tabTextView.getTypeface(), Typeface.NORMAL);
}
@Override
public void onTabReselected(TabLayout.Tab tab) { }
});
Run Code Online (Sandbox Code Playgroud)
小智 5
我知道这是一个老问题,但我想出了一个更好的解决方案:我创建了这个 OnTabSelectedListener
class OnTabSelectedBoldListener : TabLayout.OnTabSelectedListener {
override fun onTabReselected(tab: TabLayout.Tab) {}
override fun onTabUnselected(tab: TabLayout.Tab) {
val views = arrayListOf<View>()
tab.view.findViewsWithText(views, tab.text, View.FIND_VIEWS_WITH_TEXT)
views.forEach { view ->
if (view is TextView) {
TextViewCompat.setTextAppearance(view, R.style.TabTextAppearance)
}
}
}
override fun onTabSelected(tab: TabLayout.Tab) {
val views = arrayListOf<View>()
tab.view.findViewsWithText(views, tab.text, View.FIND_VIEWS_WITH_TEXT)
views.forEach { view ->
if (view is TextView) {
TextViewCompat.setTextAppearance(view, R.style.TabTextAppearance_Selected)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
小智 3
有一种方法可以通过使用 Tab CustomView、将 TextView 加载到该 CustomView 中并在 TextView 上应用样式来以编程方式添加粗体:
private TabLayout mTabLayout;
protected void onCreate(Bundle savedInstanceState) {
...
mTabLayout = (TabLayout) findViewById(R.id.tablayout);
mTabLayout.setOnTabSelectedListener(new OnTabSelectedListener());
int tabCount = mTabLayout.getTabCount();
for (int i = 0; i < tabCount; i++) {
TabLayout.Tab tab = mTabLayout.getTabAt(i);
if (tab != null) {
TextView tabTextView =
(TextView) LayoutInflater.from(this).inflate(R.layout.tab_item, mTabLayout, false);
tabTextView.setText(tab.getText());
// First tab is the selected tab, so if i==0 then set Tabs_Selected style
tabTextView.setTextAppearance(getAppContext(), i == 0 ? R.style.TextAppearance_Tabs_Selected
: R.style.TextAppearance_Tabs);
tab.setCustomView(tabTextView);
}
}
}
class OnTabSelectedListener implements TabLayout.OnTabSelectedListener {
public void onTabSelected(TabLayout.Tab selectedTab) {
int tabCount = mTabLayout.getTabCount();
for (int i = 0; i < tabCount; i++) {
TabLayout.Tab tab = mTabLayout.getTabAt(i);
View tabView = tab != null ? tab.getCustomView() : null;
if (tabView instanceof TextView) {
((TextView) tabView).setTextAppearance(getAppContext(), selectedTab.equals(tab)
? R.style.TextAppearance_Tabs_Selected
: R.style.TextAppearance_Tabs);
}
}
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
Run Code Online (Sandbox Code Playgroud)
这是 styles.xml 中的条目:
<style name="TextAppearance.Tabs" parent="TextAppearance.Design.Tab">
<item name="android:textSize">12sp</item>
<item name="android:textColor">@android:color/white</item>
</style>
<style name="TextAppearance.Tabs.Selected">
<item name="android:textStyle">bold</item>
</style>
Run Code Online (Sandbox Code Playgroud)
这是布局 tab_item:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/tab_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Tab 1" />
Run Code Online (Sandbox Code Playgroud)