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에 감사드립니다).
그러나 이처럼 텍스트 색상을 설정해도 아무런 효과가 없으므로 버튼의 텍스트 색상이 버튼의 배경색보다 어둡게 보입니다.
누구든지 다음에 무엇을 시도 할 지에 대해 조언을 해줄 수 있다면 나는 가장 감사 할 것입니다.
당신은 안드로이드 테마와 스타일을 살펴해야합니다
다음은 지원 라이브러리의 소스 코드입니다. 이 도구를 사용하면 다양한 유형의 하나 이상의 UI 요소에 동일한 모양과 느낌을 적용 할 수 있습니다. –
방금 그것에 대해 읽었습니다. – Rachel