Android,如何为 TextView 小部件设置主题字体颜色?

jam*_*esc 3 android android-theme

我刚刚使用在我的主题中应用了黑色背景图像

<style name="AppBaseTheme" parent="Theme.Sherlock.Light">
</style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:windowBackground">@drawable/background</item>
</style>
Run Code Online (Sandbox Code Playgroud)

现在我希望将我的文本视图的颜色更改为白色,以便它们在深色背景下可见

我认为为 TextView 设置 android:textColor 会像这样

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:windowBackground">@drawable/background</item>
    <item name="android:textViewStyle">@style/Ff1TextViewStyle</item>
</style>


<style name="Ff1SpinnerViewStyle" parent="android:Widget.TextView">
    <item name="android:textColor">@color/sysWhite</item>
    <item name="android:textStyle">bold</item>
</style>
Run Code Online (Sandbox Code Playgroud)

但令我沮丧的是,这完全填满了各种各样的东西,比如微调文本和微调下拉文本、ActionBar 下拉菜单,通过将文本更改为白色背景上的白色,所有这些我之前都非常满意,而且我确定其他控件我尚未使用或尚未发现的内容很可能以白色文本结束。

我发现很难相信 android 主题如此混乱,以至于我无法自信地设置 TextView widgtes 的文本颜色而不影响我只能假设是 TextView 小部件的子部件的小部件,所以我认为它必须只是我缺乏知识是罪魁祸首,我希望并寻求对必须是共同要求的启示。

任何帮助表示赞赏。

har*_*ore 7

我不确定您是否可以仅为TextView,设置样式,因为Actionbar,Spinner和其他ViewscontainsTextView也是它们的子项,因此当您发现时,它也会影响它们。在我看来,您应该创建一种样式并将其设置为您的样式TextView。类似的东西:

 <style name="MyTextView" parent="@android:style/TextAppearance.Small">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:textColor">@android:color/white</item>
    <item name="android:textSize">12sp</item>
</style>
Run Code Online (Sandbox Code Playgroud)

并在您TextView的 xml 声明中使用它:

<TextView
   style="@style/MyTextView" />
Run Code Online (Sandbox Code Playgroud)

或者您可以做的第二件事是创建您的自定义 TextView

public class MyTextView extends TextView {

     public MyTextView(Context context){
         super(context);
         this.setTextColor(android.R.color.white);
     }
}
Run Code Online (Sandbox Code Playgroud)

并在您的 xml 文件中使用 MyTextView :

<com.my.package.name.MyTextView
   android:id="@+id/my_textview" />
Run Code Online (Sandbox Code Playgroud)

编辑:

设置android:textViewStyle影响所有其他视图的原因只是因为如果您仔细查看 android 源代码,您会发现其他视图中使用的每个 textview 样式都是android.Widget.TextView. 因此,在我看来,在这种情况下您可以做的最好的事情是创建一个样式并将其设置为您TextView的 xml样式。

希望这些信息会有所帮助!