android:textAllCaps=“false” not working for TabLayout design Support

后端 未结 13 1412
暗喜
暗喜 2020-11-30 01:27

I have set android:textAllCaps=\"false\" in my android.support.design.widget.TabLayout thought it is showing the Tab Title in All caps only.

<
相关标签:
13条回答
  • 2020-11-30 01:58

    For those who can't get working other answers.

    Defining a style is working fine when you have single line tab text. If you take a close look into the TabLayout, you'll see that it's using a field design_tab_text_size_2line when the tabs has more than one line.

    The only way I could find to effect this field is to override it in your dimen file.

    So put this in your values/dimens.xml

    <dimen name="design_tab_text_size_2line" tools:override="true">10sp</dimen>
    

    Hope it helps.

    0 讨论(0)
  • 2020-11-30 01:58

    In versions priror to 14, you need to set (as commented by Paresh Mayani):

    <style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
      <item name="tabTextAppearance">@style/MyCustomTextAppearance</item>
    </style>
    
    <style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
      <item name="textAllCaps">false</item>
    </style>
    

    But, in case of android version is equal or greater than 14, you need to set:

    <item name="android:textAllCaps">false</item>
    

    So, if you need to be compatible with versions before and after 14, you also need to create a folder values-v14, and a file styles.xml in that folder with the content:

    <?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools">
        <style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
          <item name="android:textAllCaps">false</item>
        </style>
    </resources>
    
    0 讨论(0)
  • 2020-11-30 02:00

    @Paresh Mayani answer is correct however you can create only tab style

    <style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
      <item name="textAllCaps">false</item>
    </style>
    

    And use it using

    <android.support.design.widget.TabLayout
        app:tabTextAppearance="@style/MyCustomTextAppearance"
        .../>
    
    0 讨论(0)
  • 2020-11-30 02:02

    Change: <item name="android:textAllCaps">false</item>

    With: <item name="textAllCaps">false</item>

    0 讨论(0)
  • 2020-11-30 02:08

    You can also do this in your Java code. If you are using a SlidingTabLayout look at this sample:

    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;
        }
    

    Notice that textView.setAllCaps() has true as the perimeter:

    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);
            }
    

    When I changed this to (false) it solved the problem for me:

    textView.setAllCaps(false);
    

    Also my string resource file that I use for the tabs looks like this:

    <string name="tab_title">Title with capital and smaller case</string>
    

    However if it had all caps like >TITLE WITH ALL CAPS< you would still of course get all caps in your tabs.

    I made no other changes.

    It is noteworthy that you can set textView.setAllCaps(false) too, but this made no difference in my case. I just commented out textView.setAllCaps(true).

    0 讨论(0)
  • 2020-11-30 02:09

    This Worked For Me...

    <style name="TabLayoutStyle" parent="Widget.Design.TabLayout">
        <item name="tabTextAppearance">@style/TabTextAppearance</item>
    </style>
    
    <style name="TabTextAppearance" parent="TextAppearance.Design.Tab">
        <item name="textAllCaps">false</item>
    </style>
    
    0 讨论(0)
提交回复
热议问题