how to change switch imput text color in xml?

2019-06-24 07:57发布

My text in my switch defined in a xml file won't to change it's color stay black as the activity background. I tried with the textcolor option without any success. Any ideas?

enter image description here

My xml file

<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>

2条回答
Explosion°爆炸
2楼-- · 2019-06-24 08:49

When you create an instance of your EditText or TextView in your Activity code, you can set the TextColor, or Background Color for that matter, there.

Ex.

import android.graphics.Color; // add to top of your class

TextView x = (TextView)findViewById(R.id.y);

x.setTextColor(Color.parseColor("red")); // one way
x.setTextColor(Color.rgb(255, 0, 0)); // another way

x.setBackgroundColor(Color.parseColor("colorString"));
x.setBackgroundColor(Color.rgb(red int value, green int value, blue int value));
查看更多
Root(大扎)
3楼-- · 2019-06-24 08:50

For a switch, add this to your styles.xml file:

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

Two options:

  1. add this to your layout XML file:

    android:switchTextAppearance="@style/x"
    
  2. add this to your Activity class after you've created an instance of your switch:

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

Note: path to styles.xml file: Project Folder > res > values > styles.xml

查看更多
登录 后发表回答