Android SlidingTableOut选定选项卡字体粗体
如何在幻灯片布局中使选定选项卡的字体加粗?我在设置所选标签颜色方面发现了足够多的问题,但在设置所选标签字体方面没有发现任何问题。提前感谢。在您的幻灯片布局.java中,您可以看到createDefaultTabView(上下文)函数,您可以在这里设置自定义字体:Android SlidingTableOut选定选项卡字体粗体,android,Android,如何在幻灯片布局中使选定选项卡的字体加粗?我在设置所选标签颜色方面发现了足够多的问题,但在设置所选标签字体方面没有发现任何问题。提前感谢。在您的幻灯片布局.java中,您可以看到createDefaultTabView(上下文)函数,您可以在这里设置自定义字体: protected TextView createDefaultTabView(Context context) { TextView textView = new TextView(context); textView
protected TextView createDefaultTabView(Context context) {
TextView textView = new TextView(context);
textView.setGravity(Gravity.CENTER);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
textView.setTypeface(Typeface.DEFAULT_BOLD);
textView.setTextColor(getResources().getColorStateList(R.color.main));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// If we're running on Honeycomb or newer, then we can use the Theme's
// selectableItemBackground to ensure that the View has a pressed state
TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
outValue, true);
textView.setBackgroundResource(outValue.resourceId);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
// If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
textView.setAllCaps(true);
}
int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
textView.setPadding(padding, padding, padding, padding);
return textView;
}
setTypeface位于上述代码片段的第5行中,在您的幻灯片布局.java中,您可以看到createDefaultTabView(上下文上下文)函数,您可以在这里设置自定义字体:
protected TextView createDefaultTabView(Context context) {
TextView textView = new TextView(context);
textView.setGravity(Gravity.CENTER);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
textView.setTypeface(Typeface.DEFAULT_BOLD);
textView.setTextColor(getResources().getColorStateList(R.color.main));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
// If we're running on Honeycomb or newer, then we can use the Theme's
// selectableItemBackground to ensure that the View has a pressed state
TypedValue outValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
outValue, true);
textView.setBackgroundResource(outValue.resourceId);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
// If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
textView.setAllCaps(true);
}
int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
textView.setPadding(padding, padding, padding, padding);
return textView;
}
setTypeface位于上述代码片段的第5行我最终按照如下方式管理它: 在
onPageScrolled()
方法中进行以下更改:
View selectedTitle = mTabStrip.getChildAt(position);
View leftOfSelected = mTabStrip.getChildAt(position-1);
View rightOfSelected=null;
if (position<=tabStripChildCount-1) {
rightOfSelected = mTabStrip.getChildAt(position + 1);
}
if (selectedTitle != null) {
TextView selectedText = (TextView) selectedTitle;
TextView leftOfSelectedText = (TextView) leftOfSelected;
TextView rightOfSelectedText = (TextView) rightOfSelected;
if (position > 0 && position < tabStripChildCount - 1) {
selectedText.setTypeface(Typeface.DEFAULT_BOLD);
leftOfSelectedText.setTypeface(Typeface.DEFAULT);
rightOfSelectedText.setTypeface(Typeface.DEFAULT);
} else if (position == 0) {
selectedText.setTypeface(Typeface.DEFAULT_BOLD);
rightOfSelectedText.setTypeface(Typeface.DEFAULT);
} else if (position == tabStripChildCount - 1) {
selectedText.setTypeface(Typeface.DEFAULT_BOLD);
leftOfSelectedText.setTypeface(Typeface.DEFAULT);
}
}
View selectedTitle=mTabStrip.getChildAt(位置);
视图leftOfSelected=mTabStrip.getChildAt(位置-1);
视图rightOfSelected=null;
if(位置0和位置
类似地,scrollToTab()
方法也可以使用与上述相同的代码段进行编辑。您只需将selectedTitle
替换为selectedChild
,将position
替换为tabIndex
。如果您想要更好的UI响应,也可以在onPageSelected()
方法中执行相同的操作。只需记住重新声明tabStripChildCount
干杯
编辑:
随着TabLayout的引入,这变得更容易了。请参考下面Alex Zatsepin的回答。我最终按照如下方式处理了它: 在
onPageScrolled()
方法中进行以下更改:
View selectedTitle = mTabStrip.getChildAt(position);
View leftOfSelected = mTabStrip.getChildAt(position-1);
View rightOfSelected=null;
if (position<=tabStripChildCount-1) {
rightOfSelected = mTabStrip.getChildAt(position + 1);
}
if (selectedTitle != null) {
TextView selectedText = (TextView) selectedTitle;
TextView leftOfSelectedText = (TextView) leftOfSelected;
TextView rightOfSelectedText = (TextView) rightOfSelected;
if (position > 0 && position < tabStripChildCount - 1) {
selectedText.setTypeface(Typeface.DEFAULT_BOLD);
leftOfSelectedText.setTypeface(Typeface.DEFAULT);
rightOfSelectedText.setTypeface(Typeface.DEFAULT);
} else if (position == 0) {
selectedText.setTypeface(Typeface.DEFAULT_BOLD);
rightOfSelectedText.setTypeface(Typeface.DEFAULT);
} else if (position == tabStripChildCount - 1) {
selectedText.setTypeface(Typeface.DEFAULT_BOLD);
leftOfSelectedText.setTypeface(Typeface.DEFAULT);
}
}
View selectedTitle=mTabStrip.getChildAt(位置);
视图leftOfSelected=mTabStrip.getChildAt(位置-1);
视图rightOfSelected=null;
if(位置0和位置
类似地,scrollToTab()
方法也可以使用与上述相同的代码段进行编辑。您只需将selectedTitle
替换为selectedChild
,将position
替换为tabIndex
。如果您想要更好的UI响应,也可以在onPageSelected()
方法中执行相同的操作。只需记住重新声明tabStripChildCount
干杯
编辑:
随着TabLayout的引入,这变得更容易了。请参考下面Alex Zatsepin的答案。您可以使用方法将
TabLayout.OnTabSelectedListener
添加到您的TabLayout中
addOnTabSelectedListener(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) { }
您可以使用方法将
TabLayout.OnTabSelectedListener
添加到您的TabLayout
addOnTabSelectedListener(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) { }
我很感激你的回答,但我以前试过。它将所有选项卡的字体设置为粗体。我只需要选定选项卡的粗体字体。无论如何谢谢:)我很感激你的回答,但我以前试过。它将所有选项卡的字体设置为粗体。我只需要选定选项卡的粗体字体。无论如何谢谢:)谢谢!随着TabLayout的引入,这变得非常容易。我将编辑我的答案。@Alexey如何确保默认选中的选项卡也是粗体的?看起来AddOnAbsSelectedListener在视图显示后被调用,因此默认的selected选项卡不会调用它method@saintjab只需在开始添加选项卡之前添加侦听器!随着TabLayout的引入,这变得非常容易。我将编辑我的答案。@Alexey如何确保默认选中的选项卡也是粗体的?看起来AddOnAbsSelectedListener在视图显示后被调用,因此默认的selected选项卡不会调用它method@saintjab只需在开始添加选项卡之前添加侦听器