您也可以在 Java 代码中执行此操作。如果您使用的是 SlidingTabLayout,请查看此示例:
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);//see line 38 above change the value their in TAB_VIEW_TEXT_SIZE_SP.
textView.setTypeface(Typeface.DEFAULT);//From DEFAULT_BOLD
textView.setTextColor(Color.parseColor("#536DFE"));//Text color of the words in the tabs. Indigo A200
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;
}
注意 textView.setAllCaps() 有 true 作为周长:
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);
}
当我将其更改为 (false) 时,它为我解决了问题:
textView.setAllCaps(false);
我用于选项卡的字符串资源文件看起来像这样:
<string name="tab_title">Title with capital and smaller case</string>
但是,如果它有像 >TITLE WITH ALL CAPS
我没有做其他更改。
值得注意的是,您也可以设置 textView.setAllCaps(false),但这对我的情况没有任何影响。我刚刚注释掉了 textView.setAllCaps(true)。