首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ShapeDrawable获取textView背景色

使用ShapeDrawable获取textView背景色
EN

Stack Overflow用户
提问于 2013-05-23 00:50:15
回答 1查看 3.5K关注 0票数 2

我有一个在xml文件中定义了背景的文本视图。

代码语言:javascript
复制
          <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>

我需要一次性获得视图所具有的颜色。

代码语言:javascript
复制
 ShapeDrawable sc = (ShapeDrawable)tvColor.getBackground();
  ...............

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

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

代码语言:javascript
复制
        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); 

如果有人有更好的解决方案,可以说。

EN

回答 1

Stack Overflow用户

发布于 2013-05-23 00:54:10

经过进一步的研究,目前还没有办法让xml加载ShapeDrawable的颜色。你要做的就是跟踪你的颜色变化,这样你就知道你要把它设置成什么颜色,例如:

代码语言:javascript
复制
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);
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16697407

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档