【问题标题】:how to change switch imput text color in xml?如何更改xml中的开关输入文本颜色?
【发布时间】:2013-08-01 05:02:43
【问题描述】:

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

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

【问题讨论】:

  • 屏幕是不是全黑了?
  • 是的,我不喜欢白色背景

标签: android xml switch-statement textcolor


【解决方案1】:

对于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. 在您创建switch 的实例后,将其添加到您的 Activity 类中:

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

注意:styles.xml 文件的路径:Project Folder &gt; res &gt; values &gt; styles.xml

【讨论】:

  • 最后一个问题“@android:style/TextAppearance.Small”是什么?
  • @Wildchild7 会将字体大小从 medium 更改为 small。您可能可以摆脱 parent=... 行,或者只是将其设为中等。如果您有不同的theme,您可能必须修改该行以合并您的theme。但是,您最终使该行适合您,编辑我的答案或将其作为评论发布在这里,以便其他查看此内容的人也可以这样做。
【解决方案2】:

当您在Activity 代码中创建EditTextTextViewinstance 时,您可以在此处设置TextColorBackground Color

例如。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-09-24
    • 2020-04-24
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 2020-09-08
    • 1970-01-01
    相关资源
    最近更新 更多