Get a textView background color with ShapeDrawable

女生的网名这么多〃 提交于 2020-01-05 02:57:08

问题


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

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.


回答1:


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


来源:https://stackoverflow.com/questions/16697407/get-a-textview-background-color-with-shapedrawable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!