12

我可以通过在这样的对象中设置按钮来更改按钮文本的外观:

<Button
        android:id="@+id/login_btn_bypass"
        android:textSize="15dp"
        android:textColor="#878787"
        android:textStyle="bold" />

但不是在样式中使用 textAppearance 时

// in layout xml
<Button
    android:id="@+id/login_btn_login"
    android:textAppearance="@style/login_button_text_appearance" />

// in style definition 
<style name="login_button_text_appearance">
    <item name="android:textSize">15dp</item>
    <item name="android:textColor">#a7a7a7</item>
    <item name="android:textStyle">bold</item>
</style>

有谁知道为什么?

4

2 回答 2

14

使用定义的textAppearance属性值在样式中的属性值之前应用。AButtonTextView应用了样式的 a ,并且 Button 的默认样式将覆盖您的 textAppearance (例如,Android 2.3 会将其设置为?android:attr/textAppearanceSmallInverse)和textColor.

textAppearance接受样式作为值;android:textAppearance="@style/login_button_text_appearance"是设置 a 的通常正确方法textAppearance,但不适用于 a Button

如果您要更改 a 的文本颜色Button,您还应该强制使用自定义背景图像,因为如果您不这样做,一台设备将使用深色背景图像(motorola defy),而另一台设备将使用浅色图像(htc 渴望)可能会使文本难以阅读。

于 2011-11-23T17:08:41.347 回答
11

我认为你应该使用:

style = "@style/login_button_text_appearance"

代替:

android:textAppearance="@style/login_button_text_appearance"

与任何其他属性( 、等)android:textAppearance一样, 只是一个属性,并且样式的值不能作为该属性的值。android:textSizeandroid:textStyle

编辑 :

<Button
    android:id="@+id/login_btn_login"
    style="@style/login_button_text_appearance" />
于 2011-11-23T16:07:43.293 回答