简体   繁体   English

我无法在6.0以下的任何版本的android中设置TextView颜色

[英]I can't set TextView color in any version of android under 6.0

I am using the android support library. 我正在使用android支持库。 In my XML if I use TextView the text color will not change in any version of android other than android 6.0. 在我的XML中,如果我使用TextView,则文本颜色在android 6.0以外的任何其他android版本中都不会更改。 I have the problem on multiple screen layouts. 我在多个屏幕布局上都有问题。

    <TextView
        android:id="@+id/tvStatus"
        android:textColor="#FFFFFF00"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:text="@string/ted240" />

If I use android.widget.TextView everything works as it should on older version of android. 如果我使用android.widget.TextView,那么一切都会在旧版android上正常运行。

    <android.widget.TextView
        android:id="@+id/tvStatus"
        android:textColor="#FFFFFF00"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:text="@string/ted240" />

I updated the support library and gradle recently. 我最近更新了支持库和gradle。 I'm not sure if that caused the problem. 我不确定这是否引起了问题。 If I try changing them back I still have the same problem. 如果我尝试将它们改回来,我仍然会遇到同样的问题。

use below six digit hexa number only not 8 只能使用六位数以下的六进制数字,而不是8

  <TextView

       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:textColor="#ff0000"
   />

From Android Support Library 23, a new getColor() method has been added to ContextCompat. 从Android支持库23开始,新的getColor()方法已添加到ContextCompat。

But you can use it like below method which I use, 但是您可以像下面使用的方法一样使用它,

public static int getColorWrapper(Context context, int id) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        return context.getColor(id);
    } else {
        //noinspection deprecation
        return context.getResources().getColor(id);
    }
}

Then You need to call like below, 然后,您需要像下面这样打电话,

 textView.setTextColor(getColorWrapper(context,R.color.black));

Hope this will work for you. 希望这对您有用。

I tried to clean and then rebuild the project and nothing seemed to fix it. 我试图清理然后重建项目,似乎没有任何修复。 I tried changing it all the way back to 'com.android.support:appcompat-v7:22'. 我尝试将其更改回“ com.android.support:appcompat-v7:22”。

I seem to have fixed it by changing to 我似乎已经通过更改为

compile 'com.android.support:appcompat-v7:23.1.1'
compile 'com.android.support:design:23.1.1'

and then once it started working again I changed to 然后一旦它再次开始工作,我改为

compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'

and everything seems fine. 一切似乎都很好。 I am confused but happy it works. 我很困惑,但很高兴它能奏效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM