android - 为什么应用程序崩溃,当我尝试通过自定义样式设置文本大小属性时

标签 android xml android-layout textview android-styles

当我尝试将自定义样式应用到我的 TextView,并尝试不通过 sp 中的值指定 textSize 属性时,我遇到了应用程序崩溃。代码如下。

<style name="TextViewRegistrationStyle">
    <item name="android:textSize">@android:style/TextAppearance.Large</item>
    <item name="android:textStyle">bold</item>
    <item name="android:gravity">center</item>
    <item name="android:background">?android:attr/selectableItemBackground</item>
</style>

异常(exception):

Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x1

当我以这种方式指定值时:

<style name="TextViewRegistrationStyle">
    <item name="android:textSize">16sp</item>
    <item name="android:textStyle">bold</item>
    <item name="android:gravity">center</item>
    <item name="android:background">?android:attr/selectableItemBackground</item>
</style>

一切正常。为什么会这样?

最佳答案

textSize是简单的维度属性,textAppearance是由多个值组成的复杂属性。您会收到错误消息,因为这两者不兼容。

textSize声明:<attr name="textSize" format="dimension" />

textAppearance声明:

<declare-styleable name="TextAppearance">
    <!-- Text color. -->
    <attr name="textColor" />
    <!-- Size of the text. Recommended dimension type for text is "sp" for scaled-pixels (example: 15sp). -->
    <attr name="textSize" />
    <!-- Style (bold, italic, bolditalic) for the text. -->
    <attr name="textStyle" />
    <!-- Typeface (normal, sans, serif, monospace) for the text. -->
    <attr name="typeface" />
    <!-- Font family (named by string) for the text. -->
    <attr name="fontFamily" />
    <!-- Color of the text selection highlight. -->
    <attr name="textColorHighlight" />
    <!-- Color of the hint text. -->
    <attr name="textColorHint" />
    <!-- Color of the links. -->
    <attr name="textColorLink" />
    <!-- Present the text in ALL CAPS. This may use a small-caps form when available. -->
    <attr name="textAllCaps" format="boolean" />
    <!-- Place a blurred shadow of text underneath the text, drawn with the
         specified color. The text shadow produced does not interact with
         properties on View that are responsible for real time shadows,
         {@link android.R.styleable#View_elevation elevation} and
         {@link android.R.styleable#View_translationZ translationZ}. -->
    <attr name="shadowColor" format="color" />
    <!-- Horizontal offset of the text shadow. -->
    <attr name="shadowDx" format="float" />
    <!-- Vertical offset of the text shadow. -->
    <attr name="shadowDy" format="float" />
    <!-- Blur radius of the text shadow. -->
    <attr name="shadowRadius" format="float" />
    <!-- Elegant text height, especially for less compacted complex script text. -->
    <attr name="elegantTextHeight" format="boolean" />
    <!-- Text letter-spacing. -->
    <attr name="letterSpacing" format="float" />
    <!-- Font feature settings. -->
    <attr name="fontFeatureSettings" format="string" />
</declare-styleable>

您可以做的是创建您自己的修改后的文本外观样式并将其分配给 textAppearance而不是 textSize .

<style name="TextAppearance.Registration" parent="android:TextAppearance.Large">
    <item name="android:textStyle">bold</item>
</style>

<style name="TextViewRegistrationStyle">
    <item name="android:textAppearance">@style/TextAppearance.Registration</item>
    <item name="android:gravity">center</item>
    <item name="android:background">?android:attr/selectableItemBackground</item>
</style>

关于android - 为什么应用程序崩溃,当我尝试通过自定义样式设置文本大小属性时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33605999/

相关文章:

javascript - 关闭由Android应用程序打开的浏览器标签

android - Eclipse-Android Emulator 中Android OS 中使用的Spinner 9 补丁在哪里获取?

android - CardView 内的 ImageView CardView 内

android - 优先安排崩溃的 android 服务的重启

java - Android IO 慢?

c# - 使用 Xml.Xpath 和 Xml.Linq 读取 XML 文件

xml - 转换为显示样式表行的 xsl

xml - 文档 xi :include

android - 如何解决可能的剪切和粘贴错误。?

java - 如何以编程方式连续绘制多个矩形?