为什么应用程序崩溃,当我尝试通过自定义样式设置文本大小属性时
Why the app crashes, when I trying to set a text size attribute via custom style
当我尝试将自定义样式应用到我的 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>
当我尝试将自定义样式应用到我的 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>