在Android应用中更改样式和主题中的文本外观

27

我正在编写一款安卓应用程序,想要改变它的主题。

我已经通过使用一个主题来改变了应用程序的背景颜色,但是我无法更改默认文本外观。

这是我的styles.xml资源文件。

<resources>
    <style name="QTheme" parent="android:Theme.Light">
        <item name="android:windowBackground">@color/q_green</item>
        <item name="android:colorBackground">@color/q_green</item>
        <item name="android:textAppearance">@style/QText</item> 
    </style>

    <color name="q_green">#008000</color>
    <color name="white">#FFFFFF</color>

    <style name="QText" parent="@android:style/TextAppearance.Medium"> 
        <item name="android:textSize">20sp</item> 
        <item name="android:textColor">@color/white</item> 
        <item name="android:textStyle">bold</item>
        <item name="android:textFont">Verdana</item>
    </style>
</resources>

仅作检查,这是我其中一个TextViews的示例,我想在我的布局文件中使用上述样式。

<TextView
    android:id="@+id/txtDevice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="@string/device" 
/>

有谁能看出我错在哪里了吗?


6
你刚刚在主题中覆盖了TextAppearance.Medium样式(这不会修改TextView的外观)。如果你想修改TextView的外观,则应该在主题中覆盖textViewStyle样式属性。或者你可以使用android:textAppearance来应用你的自定义样式。 - user
谢谢。我将项目名称更改为android.textViewStyle,这似乎解决了问题。但它也更改了我的下拉框中的文本,而不仅仅是我的文本视图。有没有办法区分它们? - Pippa Rose Smith
我不确定。如果我没记错的话,SpinnerTextView项目具有不同的样式(仍继承自TextView样式),由主题中的spinnerItemStyle属性指定。您可能希望将其设置为@android:style/Widget.TextView.SpinnerItem,然后查看效果如何。 - user
2个回答

47

为了完整性,并且全面肯定Lusprog的贡献,这是现在我的资源文件。

<resources>
    <style name="QTheme" parent="android:Theme.Light">
        <item name="android:windowBackground">@color/q_green</item>
        <item name="android:colorBackground">@color/q_green</item>
        <item name="android:textViewStyle">@style/QText</item> 
    </style>

    <color name="q_green">#008000</color>
    <color name="white">#FFFFFF</color>

    <style name="QText" parent="@android:style/TextAppearance.Medium"> 
        <item name="android:textSize">20sp</item> 
        <item name="android:textColor">@color/white</item> 
        <item name="android:textStyle">bold</item>
        <item name="android:typeface">sans</item>
    </style>
</resources>

同时感谢Padma Kumar提供的字体帮助。


你改了什么?除了android:typeface! - Padma Kumar
1
<item name="android:textViewStyle">@style/QText</item> - Pippa Rose Smith
你好,能告诉我在安卓系统中是否有android:textViewStyle这个属性吗? - Padma Kumar
不太明白你的问题,但如果你的意思是android:textViewStyle是Android中的一个属性,那么是的,它似乎是。 - Pippa Rose Smith
如何在样式中设置文本背景颜色?如果我使用android:background color进行设置,它不起作用,尽管BackgroundColorSpan可以解决问题。 - Piotr

3

//请将此行从您的样式表中删除 没有这样的属性

<item name="android:textFont">Verdana</item>

//您可以为更改字体样式提供字体

<item name="android:typeface">monospace</item>

//用于设置样式

<TextView
    android:id="@+id/txtDevice"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    style="@style/QText"
    android:text="@string/device" 
/>

2
有没有办法将样式添加到主题中,这样我就不必为每个文本设置样式了? - Pippa Rose Smith
不,你不能通过 XML 设置字体样式。需要通过代码设置字体。 - Padma Kumar

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接