android - 如何更改 xml 中的开关输入文本颜色?

标签 android xml switch-statement textcolor

我在 xml 文件中定义的 switch 中的文本不会改变它的颜色保持黑色作为 Activity 背景。我尝试使用 textcolor 选项但没有成功。有什么想法吗?

enter image description here

我的xml文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">

    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hôte : "
            android:textColor="#FFFFFF"/>
        <EditText 
            android:background="#40FFFFFF"
            android:id="@+id/hostname"
            android:layout_width="200px"
            android:layout_height="wrap_content"
            android:textColor="#FFFFFF"/>

    </LinearLayout>

    <LinearLayout 
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Utiliser Https : "
            android:textColor="#FFFFFF"/>
        <Switch 
            android:id="@+id/Switch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOn="on"
            android:textOff="off"
            android:textColor="#FFFFFF"
            android:onClick="onToggleClicked"/>

    </LinearLayout>
</LinearLayout>

最佳答案

对于 switch,将其添加到您的 styles.xml 文件中:

<style name="x" parent="@android:style/TextAppearance.Small">
    <item name="android:textColor">#33CCFF</item>
</style>

两种选择:

  1. 将此添加到您的布局 XML 文件中:

    android:switchTextAppearance="@style/x"
    
  2. 在您创建了您的开关实例之后,将其添加到您的 Activity 类中:

    switchInstance.setSwitchTextAppearance(getActivity(), R.style.x);
    

注意styles.xml 文件的路径:Project Folder > res > values > styles.xml

关于android - 如何更改 xml 中的开关输入文本颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17974305/

相关文章:

php - switch函数中有 "Else Case"可以使用吗?

c++ - 为什么 switch 语句不能应用于字符串?

android - OBD2 - ELM327 蓝牙模拟器

xml - XSLT 复制除 1 个元素之外的所有节点

c - 使用 libxml2 读取 XML

java - 如何删除输入xml中的空标签

switch-statement - 高效开关需要自认证吗?

android - 如何在android中停止计时器?

java - 如何在 Intent 中插入 fragment (Android)

android - Google Maps API - 从郊区的起点到终点标记一条街道