当我尝试将自定义样式应用于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>


例外:

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/

10-10 00:25