【问题标题】:setTextColor on Android 5.1安卓 5.1 上的 setTextColor
【发布时间】:2016-01-14 06:32:58
【问题描述】:

为什么只在 Android 5.1 或更高版本上setTextColor 方法不起作用?

TextView TextView32 = (TextView) findViewById(R.id.textView32);
TextView32.setText(String.valueOf(LikesNumber2));
SendLikes2.setBackgroundResource(R.drawable.likeyes);
TextView32.setTextColor(Color.parseColor("#ffffff"));

奇怪,但在 Android 4.4 及以上的 30 款手机上,可变版本的 textColor 发生了变化。哪里出错了?

XML:

<RelativeLayout                
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:layout_marginBottom="7dp">

     <Button
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/SendLikesHe"
            android:background="@drawable/likezero"
            android:layout_gravity="center_horizontal"
            android:layout_marginTop="15dp"
            android:layout_centerHorizontal="true" />

     <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView32"
            android:textColor="#fffb0049"
            android:layout_centerHorizontal="true"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="14dp"
            android:layout_alignBottom="@+id/SendLikesHe" />

     <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Like"
            android:id="@+id/textView67_onn"
            android:layout_gravity="center_horizontal"
            android:textSize="13sp"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="false"
            android:layout_below="@+id/SendLikesHe"
            android:textColor="#FB0049" />
</RelativeLayout>

【问题讨论】:

  • 奇怪。它为我工作 android 5.1.1
  • 使用ContextCompat.getColor(),见here

标签: android text colors textview android-5.1.1-lollipop


【解决方案1】:

你可以使用

TextViewObj.setTextColor(getResources().getColor(R.color.Color_Name));

或使用 ContextCompat

TextViewObj.setTextColor(ContextCompat.getColor(context,R.color.Color_Name));

getColor(int id) deprecated on Android 6.0 Marshmallow (API 23)

希望这会有所帮助。

【讨论】:

  • 感谢您的帮助,但我认为问题出在 ImageView 中。它可以强加文本(我不知道我怎么能用英语说 - 比如“你看不到文本,因为它们上有图像视图”)。所以问题没有解决
【解决方案2】:

你正在尝试

 TextView32.setTextColor(Color.parseColor("#ffffff"));

这是WHITE 颜色。你的背景颜色不是white吗??

尝试将背景颜色设置为灰色(#808080)或其他,并将颜色设置为,

TextViewObj.setTextColor(getResources().getColor(R.color.Color_Name));

【讨论】:

    【解决方案3】:
    textView.setTextColor(Color.parseColor("#DD1515"));
    

    上面的行应该可以正常工作。在 4.4、5.1、6.0 中测试

    【讨论】:

      猜你喜欢
      • 2016-05-28
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 2023-03-10
      • 2010-09-28
      • 2011-11-05
      相关资源
      最近更新 更多