我想让所选标签的文字为粗体。我可以通过xml或java代码来做到这一点,无论哪种方式更简单。如何在使用tabLayout时更改选定选项卡的文本样式?
5
A
回答
1
有加大胆编程使用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) {
}
这里是在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>
这里是布局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" />
9
我改变了答案建议上面有点,它对我很好,没有额外的.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) {
}
});
1
如果使用默认TabLayout(不customView),您可以通过使用getChildAt()方法获取标签的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) { }
});
+2
这将字体设置为粗体。但是在取消选择时不会将其返回到正常字体? –
相关问题
- 1. 更改TabLayout的选定选项卡背景和文本颜色
- 2. Android:tabSelectedTextColor不会更改TabLayout中选定选项卡的文本
- 3. 如何更改tablayout中选定选项卡的颜色?
- 4. 更改选项卡选择选项卡时的文本颜色
- 5. 无法更改TabLayout中的选项卡
- 6. Ionic2选项卡 - 如何更改选定选项卡的颜色?
- 7. JavaFX CSS样式:更改选定选项卡的高亮颜色
- 8. 仅在选定模式下更改选定选项的文本
- 9. 如何监听TabLayout中的选项卡更改?
- 10. 如何在访问者选项卡选择元素时更改样式?
- 11. 如何使用选择选项更改div的样式
- 12. 如何在wordpress中将文本选项卡更改为html选项卡
- 13. 使用样式表更改索引QTabBar选项卡的背景
- 14. JTabbedPane:更改选项卡标题时更改选项卡大小
- 15. 如何更改React-native中的样式可滚动选项卡
- 16. 如何在Internet Explorer的选项卡容器中使用javascript更改选项卡
- 17. 如何在页面加载时更改活动选项卡的样式
- 18. 如何更改滑动选项卡中选定的选项卡颜色?
- 19. react-native-router-flux选项卡如何更改选定选项卡的图标?
- 20. 更改选项卡式内容部分中选定选项卡的外观
- 21. 如何更改iphone中Tabbar中的选定选项卡项?
- 22. 如何用代码更改BottomNavigationView的选定选项卡?
- 23. 选项卡没有选择与选定的形式更改
- 24. 我想知道如何更改选项卡控件样式
- 25. 选项卡不填充tablayout
- 26. 使用styles.xml的样式选项卡
- 27. jQuery UI选项卡 - 如何更改选项卡的位置
- 28. 以编程方式更改TabLayout的选项卡,但ViewPager的片段未更新
- 29. TabLayout选定的指标 - 必须选择选项卡两次
- 30. 如何在选择时更改颜色选项卡
我该如何在XML代码中做到这一点? –