android:textAllCaps =“false”不适用于TabLayout设计支持

时间:2015-07-08 14:05:22

标签: android androiddesignsupport android-tablayout

我已在我的android:textAllCaps="false"中设置android.support.design.widget.TabLayout,因为它只显示所有大写字母标题标题。

如何删除所有大写字母?

15 个答案:

答案 0 :(得分:129)

设计资料库更新23.2.0 +

原始答案不适用于设计库23.2.0或更高版本。感谢@ fahmad6在评论中指出,如果有人错过了评论,我会把它放在这里。您需要将textAllCapsandroid:textAllCaps都设置为false以停用所有大写设置。

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

原始回答

默认情况下,TabLayout创建的选项卡将textAllCaps属性设置为true,您必须定义一个样式,使此标志为false。

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

答案 1 :(得分:48)

@Paresh Mayani答案是正确的,但您只能创建标签样式

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

使用

使用它
<android.support.design.widget.TabLayout
    app:tabTextAppearance="@style/MyCustomTextAppearance"
    .../>

答案 2 :(得分:26)

https://stackoverflow.com/a/34678235/1025379

<android.support.design.widget.TabLayout
    app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
/>

答案 3 :(得分:14)

使用此属性app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget" 它会起作用。

  <android.support.design.widget.TabLayout
    android:id="@+id/tablayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    app:tabGravity="fill"
    app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
    app:tabIndicatorColor="@color/colorPrimary"
    app:tabMode="fixed"
    app:tabPaddingStart="0dp" />

答案 4 :(得分:4)

这是一个简单的解决方案....享受

    $name = array("page18.jpg","page16.jpg","page17.jpg","page19.jpg",);
    foreach ($name as $n) {
        $num_arr[] = filter_var($n, FILTER_SANITIZE_NUMBER_INT);
    }

    var_dump($num_arr);
    exit();

答案 5 :(得分:4)

对于那些无法解决其他问题的人

当您有单行标签文本时,定义样式工作正常。 如果您仔细查看TabLayout,当标签包含多行时,您会看到它使用字段 design_tab_text_size_2line

我能找到影响此字段的唯一方法是在你的维度文件中覆盖它。

所以把它放在你的values / dimens.xml

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

希望它有所帮助。

答案 6 :(得分:2)

在版本priror到14中,你需要设置(由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>

但是,如果Android版本等于或大于14,则需要设置:

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

因此,如果您需要与14之前和之后的版本兼容,那么需要在该文件夹中创建文件夹values-v14和文件styles.xml,其内容为:< / p>

<?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>

答案 7 :(得分:2)

就我而言,有两种变体可以工作:

1)博格丹(susemi99):

<android.support.design.widget.TabLayout
    app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"
/>

2)通过Paresh Mayani。我想同时拥有android:textAllCaps="false"android:textSize="15sp",所以他的旧方法行得通。

styles.xml中写(父母可能有所不同,例如“ @android:style / TextAppearance.Widget.TabWidget”,“ TextAppearance.Design.Tab”):

<style name="TabLayout" parent="Widget.Design.TabLayout">
    <item name="tabIndicatorColor">@color/color_blue</item>
    <item name="tabSelectedTextColor">@color/color_blue</item>
    <item name="tabTextColor">@color/black</item>
    <item name="tabTextAppearance">@style/TabLayoutTextAppearance</item>
</style>

<style name="TabLayoutTextAppearance" parent="TextAppearance.Design.Tab">
    <item name="android:textSize">15sp</item>
    <item name="textAllCaps">false</item>
    <item name="android:textAllCaps">false</item>
</style>

在布局中应用此样式:

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/TabLayout"
    />

答案 8 :(得分:2)

这对我来说只需要一行

 <android.support.design.widget.TabLayout
    app:tabTextAppearance="@android:style/TextAppearance.Widget.TabWidget"/>

答案 9 :(得分:1)

尝试以下方法,您可以在TextView

中实施TabLayout的所有方法
private void setCustomTab() {

    ViewGroup vg = (ViewGroup) mTabLayout.getChildAt(0);
    int tabsCount = vg.getChildCount();
    for (int j = 0; j < tabsCount; j++) {
        ViewGroup vgTab = (ViewGroup) vg.getChildAt(j);
        int tabChildsCount = vgTab.getChildCount();
        for (int i = 0; i < tabChildsCount; i++) {
            View tabViewChild = vgTab.getChildAt(i);
            if (tabViewChild instanceof TextView) {
                ((TextView) tabViewChild).setTypeface(ResourcesCompat.getFont(this,R.font.montserrat_medium));
                ((TextView) tabViewChild).setAllCaps(false);
            }
        }
    }
}

希望它有所帮助。

答案 10 :(得分:0)

您也可以在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>

但是,如果它有像​​&gt;所有大写的标题&lt; TITLE WITH ALL CAPS&lt;你当然会在你的标签中获得所有上限。

我没有做任何其他改动。

值得注意的是,您也可以设置textView.setAllCaps(false),但这在我的情况下没有任何区别。我刚刚注释掉了textView.setAllCaps(true)。

答案 11 :(得分:0)

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

使用: <item name="textAllCaps">false</item>

答案 12 :(得分:0)

这对我有用...

OR

答案 13 :(得分:0)

只需将其添加到您的自定义样式文件中

<item name="android:textStyle">normal</item>

我阅读了以上所有解决方案,并得出了所有结论。

答案 14 :(得分:0)

更改 XML 文件中的属性在 Android 11 (SDK 30) 中不起作用。这是我用来使用选项卡标签单独设置选项卡的代码。这比为选项卡中的所有文本字段设置新样式或依赖现有选项卡布局更安全,因为 Android 可以更改当前的选项卡设计。此方法假定在调用以下函数之前已设置选项卡文本。调用中的第二个参数,txt 是标签标签。

private fun setTabStyle(tabs: TabLayout, txt: String) {
    val av = ArrayList<View?>()
    tabs.findViewsWithText(av, txt, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION )
    if (av.count() > 0) {
        val avt = ArrayList<View?>()
        (av[0] as? ViewGroup)?.let {
            for ( i in 0 until it.childCount) {
                val tv = it.getChildAt(i) as? TextView
                tv?.let {t ->
                    if (tv.text == txt) {
                        t.isAllCaps = false
                        t.setTextSize(TypedValue.COMPLEX_UNIT_SP, 11.toFloat())

                    }
                }

            }

        }
    }

}