Android textAllCaps in Theme

﹥>﹥吖頭↗ 提交于 2019-11-29 14:01:03

问题


I have a theme where on wanted all textviews on activities to be capitalized. So I set textAllCaps in a style and then applied it to textViewStyle in my theme like the below

<style name="Widget.Apex.TextView" parent="android:Widget.TextView">
  <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
  <item name="android:textAllCaps">true</item>
</style>

<style name="MyTheme" parent="@android:style/Theme.Holo">
  <item name="android:textViewStyle">@style/Widget.Apex.TextView</item>
</style>

Unfortunately this had the side effect of turning my Application Title, ActionBar subtitles, and popmenu items capitalized. So I thought I would just set the ActionBarStyle, textAppearanceLargePopupMenu, and textAppearanceSmallPopupMenu attributes in my theme to have a custom style with textAllCaps=false. However, this does not work. I know my themes are being applied because if I set textColor or textStyle they are both applied to the titles and menu items. Is it possible to override the textAllCaps in these styles?

<style name="Widget.Apex.TextView" parent="android:Widget.TextView">
  <item name="android:textAppearance">?android:attr/textAppearanceSmall</item>
  <item name="android:textAllCaps">true</item>
</style>
<style name="TextAppearance.Apex.Widget.ActionBar.Title" parent="android:TextAppearance.Holo.Widget.ActionBar.Title">
  <item name="android:textAllCaps">false</item>
  <item name="android:textStyle">italic</item>
</style>
<style name="TextAppearance.Apex.Widget.ActionBar.Subtitle" parent="android:TextAppearance.Holo.Widget.ActionBar.Subtitle">
  <item name="android:textAllCaps">false</item>
  <item name="android:textStyle">italic</item>
  <item name="android:textColor">#FF0000</item>
</style>
<style name="TextAppearance.Apex.Widget.PopupMenu.Large" parent="android:TextAppearance.Holo.Widget.PopupMenu.Large">
  <item name="android:textAllCaps">false</item>
  <item name="android:textStyle">italic</item>
  <item name="android:textColor">#FF0000</item>
</style>
<style name="TextAppearance.Apex.Widget.PopupMenu.Small" parent="android:TextAppearance.Holo.Widget.PopupMenu.Small">
  <item name="android:textAllCaps">false</item>
  <item name="android:textStyle">italic</item>
  <item name="android:textColor">#FF0000</item>
</style>
<style name="Widget.Apex.ActionBar" parent="android:Widget.Holo.ActionBar">
  <item name="android:titleTextStyle">@style/TextAppearance.Apex.Widget.ActionBar.Title</item>
  <item name="android:subtitleTextStyle">@style/TextAppearance.Apex.Widget.ActionBar.Subtitle</item>
</style>

<style name="MyTheme" parent="@android:style/Theme.Holo">
  <item name="android:actionBarStyle">@style/Widget.Apex.ActionBar</item>
  <item name="android:textAppearanceLargePopupMenu">@style/TextAppearance.Apex.Widget.PopupMenu.Large</item>
  <item name="android:textAppearanceSmallPopupMenu">@style/TextAppearance.Apex.Widget.PopupMenu.Small</item>
  <item name="android:textViewStyle">@style/Widget.Apex.TextView</item>
</style>

Thanks, Jonathan


回答1:


By using AppCompat textAllCaps in Android Apps supporting older API's (less than 14)

A work around for this would to explicitly set the style like this:

<android.support.v7.internal.widget.CompatTextView
        style="@style/MyTextViewStyle"
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:textAllCaps="true"/>

or alternatively use a new style that inherits textViewStyle and adds the textAllCaps attribute along with any other styles of your choosing:

  <style name="MyCompatTextViewStyle" parent="MyTextViewStyle">
            <item name="textAllCaps">true</item>
        </style>

In layout:

<android.support.v7.internal.widget.CompatTextView
            style="@style/MyCompatTextViewStyle"
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

Note:

1> this widget won't inherit any default styles that are set in your theme via android:textViewStyle

2>Using this widget seems to be internal given the package name meaning it's not really intended for public API. It's also not "fully baked" in that the textAllCaps attribute needs to be directly on the style/view and can not be in the textAppearance style for it to work.




回答2:


Try with both textAllCaps and android:textAllCaps.

<style name="TextViewStyle">
      <item name="textAllCaps">true</item>
      <item name="android:textAllCaps">true</item>
</style>

But I think it's working just with design library 23.2.0 or later.



来源:https://stackoverflow.com/questions/11215326/android-textallcaps-in-theme

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!