2010-07-22 13 views
9

REST API에서 일부 데이터를 읽고 앱에서받는 정보를 기반으로 버튼을 생성해야합니다.Android - 프로그래밍 방식으로 버튼 색상을 설정하는 방법

많은 활동 화면에서 동일한 버튼이 필요하므로 RachelButton을 만들기 위해 버튼을 확장하고 생성자에서 설정했습니다.

public RachelButton(Context context, Info info) { 
    super(context); 
    this.info= info; 

    setText(info.getTime()); 
    setTypeface(Typeface.DEFAULT, Typeface.BOLD); 

    int identifier = 0; 

    if(info.isAvailable()){ 
     identifier = getContext().getResources().getIdentifier("drawable/info_button_"+info.getType(), null, getContext().getPackageName()); 
    }else{ 
     identifier = R.drawable.info_button_unavailable; 
    } 

    if(identifier == 0){ 
     Log.e("INFO_BUTTON", "no button for "+info.getType()); 
    } 

    setBackgroundResource(identifier); 
    setTextColor(R.color.info_button_text_color); 

    setOnClickListener(new View.OnClickListener(){ 
     public void onClick(View view) { 
      //do stuff 
     } 
    }); 
} 

는 그럼 난 컬러 버튼을 생성하기 위해 사용하고있는 자원의 예는 이것이다 :

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<item android:state_pressed="true" > 
    <shape> 
     <gradient 
      android:startColor="@color/button_pressed" 
      android:endColor="@color/button_pressed" 
      android:angle="270" /> 
     <stroke 
      android:width="3dp" 
      android:color="@color/button_pressed" /> 
     <corners 
      android:radius="3dp" /> 
     <padding 
      android:left="5dp" 
      android:top="5dp" 
      android:right="5dp" 
      android:bottom="5dp" /> 
    </shape> 
</item> 

<item android:state_focused="true" > 
    <shape> 
     <gradient 
      android:endColor="@color/info_normal" 
      android:startColor="@color/info_normal" 
      android:angle="270" /> 
     <stroke 
      android:width="3dp" 
      android:color="@color/info_normal" /> 
     <corners 
      android:radius="3dp" /> 
     <padding 
      android:left="5dp" 
      android:top="5dp" 
      android:right="5dp" 
      android:bottom="5dp" /> 
    </shape> 
</item> 

<item> 
    <shape> 
     <gradient 
      android:endColor="@color/info_normal" 
      android:startColor="@color/info_normal" 
      android:angle="270" /> 
     <stroke 
      android:width="3dp" 
      android:color="@color/info_normal" /> 
     <corners 
      android:radius="3dp" /> 
     <padding 
      android:left="5dp" 
      android:top="5dp" 
      android:right="5dp" 
      android:bottom="5dp" /> 
    </shape> 
</item> 
</selector> 

내가 텍스트 색상을 설정하고 코드에서 볼 수 있으며, 그 확신으로 이 색상은 자원으로 존재합니다 (IntelliJ에 감사드립니다).

그러나 이처럼 텍스트 색상을 설정해도 아무런 효과가 없으므로 버튼의 텍스트 색상이 버튼의 배경색보다 어둡게 보입니다.

누구든지 다음에 무엇을 시도 할 지에 대해 조언을 해줄 수 있다면 나는 가장 감사 할 것입니다.

+0

당신은 안드로이드 테마와 스타일을 살펴해야합니다
다음은 지원 라이브러리의 소스 코드입니다. 이 도구를 사용하면 다양한 유형의 하나 이상의 UI 요소에 동일한 모양과 느낌을 적용 할 수 있습니다. –

+0

방금 ​​그것에 대해 읽었습니다. – Rachel

답변

43

당신은 수행해야합니다

setTextColor(getContext().getResources().getColor(R.color.info_button_text_color)); 
+0

고마워요 - 그게 완벽하게 작동합니다 :) – Rachel

+0

고마워요. – SaKet

+3

** API 23 **에서 ContextCompat.getColor (context, R.color.your_color);를 사용하고 싶습니다. [getcolorint-id-deprecated-on - android-6-0-marshmallow-api-23] (http://stackoverflow.com/questions/31590714/getcolorint-id-deprecated-on-android-6-0-marshmallow-api-23) –

3

더 나은 당신이 (R 클래스 findViewById)보기 객체가있는 경우 변형 정보를 특정 개체를 예 버튼을 위해. (표준 방식 - Button b = (Button) fin...(R.id.sdfsdf))

다음 몇 가지 안드로 - 색상 입력

:

b.setTextColor(Color.rgb(0xff, 0x66, 0x33)); 

모든 이클립스에서 ctrl+spaceBar에 RGB FROM :

b.setTextColor(Color.parseColor("green")); 

또는 더 나은 P


죄송합니다. 어쩌면 b.setTextColor(0xff0000)도 작동합니다 ...

0

getColor() 함수는 API 수준 23에서 사용되지 않습니다. 자세한 내용은 link을 확인하십시오.

public static final int getColor(Context context, int id) { 
    final int version = Build.VERSION.SDK_INT; 
    if (version >= 23) { 
     return ContextCompatApi23.getColor(context, id); 
    } else { 
     return context.getResources().getColor(id); 
    } 
} 
관련 문제