获取与ShapeDrawable一个TextView背景颜色 [英] Get a textView background color with ShapeDrawable

查看:211
本文介绍了获取与ShapeDrawable一个TextView背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个XML文件中定义的背景一个TextView。

I have a textview with the background defined in a xml file.

          <TextView
            android:id="@+id/event_tvColor"
            android:layout_width="40dip"
            android:layout_height="40dip"
            android:text="   "
            android:background="@drawable/et_style_color_service_edit"
            android:clickable="true"
          />

           xml file :  et_style_color_service_edit.xml

            <?xml version="1.0" encoding="UTF-8"?>
              <shape xmlns:android="http://schemas.android.com/apk/res/android" 
                    android:shape="oval">        
                 <solid android:color="@color/eventColor"/>
                 <stroke android:width="0sp" android:color="#FFFFFF" />
                 <size android:width="20dp"
                       android:height="20dp"/>
              </shape>

,我需要得到的观点有一个时间的颜色。

And i need to get the color that the view have in a one time.

 ShapeDrawable sc = (ShapeDrawable)tvColor.getBackground();
  ...............

请注意,我需要使用ShapeDrawable而不是GradientDrawable。
感谢您的帮助和时间。

Note that i need to use ShapeDrawable and not GradientDrawable. Thank you for your help and time.

解决方案........

Solution........

        Solution The xml loads into the app as a gradientdrawable and not as a shapedrawable. We have to define the shapeDrawable in java 

          ShapeDrawable sd = new ShapeDrawable(new RectShape);  
          sd.getPaint().setColor(0xFF0000FF); 

如果任何人有一个更好的解决方案可以告诉。

if anyone have a better solution can tell.

推荐答案

经过进一步研究,目前有没有得到加载ShapeDrawable的颜色XML的方式。你所要做的仅仅是追踪您的色彩变化,所以你知道什么颜色你将它设置为,即:

After further research, there currently is no way of getting the xml loaded ShapeDrawable's color. What you have to do is just track your color changes so you know what color you are setting it to, ie:

int currentColor = Color.WHITE; //this is the default color (color set in xml)

public void changeColor() {
    if (currentColor == Color.WHITE) {
        currentColor = Color.BLUE;
    } else {
        currentColor = Color.WHITE;
    }
    GradientDrawable gd = (GradientDrawable)tvColor.getBackground();
    gd.setColor(currentColor);
}

这篇关于获取与ShapeDrawable一个TextView背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆