2013-05-22 3 views
2

xml 파일에 정의 된 배경을 가진 textview가 있습니다.ShapeDrawable을 사용하여 textView 배경색 가져 오기

  <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> 

그리고보기가 한 번에 가져올 색상을 가져와야합니다.

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

GradientDrawable이 아닌 ShapeDrawable을 사용해야한다는 점에 유의하십시오. 도와 주셔서 감사합니다. 사람이있는 경우

솔루션 ........

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

더 나은 솔루션을 알 수 있습니다.

답변

4

추가 연구가 끝나면 현재로드 된 ShapeDrawable의 색상을 가져 오는 방법이 없습니다. 당신이해야 할 일은 색상 변경을 추적하는 것입니다. 즉, 어떤 색상을 설정하고 있는지, 즉 :

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

안녕 Zed Scio. 답장을 보내 주셔서 감사합니다. 내가 시도해 ShapeDrawable sc = (ShapeDrawable) tvColor.getBackground(); int color = sc.getPaint(). getColor(); . 그러나 저에게 오류를주십시오. java.lang.ClassCastException : android.graphics.drawable.GradientDrawable android.graphics.drawable.ShapeDrawable에 캐스팅 될 수 없습니다 ... 그리고 xml 파일의 요소가 shapeDrawable 및 gradient drawable에 제공되기 때문에 이해가 안됩니다. 어떤 생각? –

+1

http://stackoverflow.com/questions/3066892/how-to-import-shape-resourcexml-to-code에 따르면 xml 이 애플리케이션에 그라데이션 드로 가능으로로드됩니다. 내 제안은 자바에서 ShapeDrawable을 만든 다음 EditText로 설정하는 것입니다. –

+0

"java에서 shapeDrawable 만들기"를 이해하지는 못합니다. 나 한테 제드 스코티 어를 줄 수있어? 도와 주셔서 감사합니다. –

관련 문제