=================================
=================================
=================================
//빨간색 계열 그라데이션 버튼
Button del = new Button(객체..);
del.getBackground().setColorFilter(new LightingColorFilter(0xFFFF5050, 0x0000000));
=================================
=================================
=================================
출처: http://stackoverflow.com/questions/1521640/standard-android-button-with-a-different-color
Standard Android Button with a different color
youDevise, Ltd. - Pragmatic Programmer London, England
EF Englishtown - Senior .Net Technical Lead / Architect Shanghai, China
Groupon GmbH - Java Frontend Developer Berlin, Germany
mag10 publishing - Software Engineers Web Frontend Berlin, Germany
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/red_button_pressed" />
<item android:state_focused="true" android:drawable="@drawable/red_button_focus" />
<item android:drawable="@drawable/red_button_rest" />
</selector>
<?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/yellow1"
android:endColor="@color/yellow2"
android:angle="270" />
<stroke
android:width="3dp"
android:color="@color/grey05" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item android:state_focused="true" >
<shape>
<gradient
android:endColor="@color/orange4"
android:startColor="@color/orange5"
android:angle="270" />
<stroke
android:width="3dp"
android:color="@color/grey05" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
<item>
<shape>
<gradient
android:endColor="@color/blue2"
android:startColor="@color/blue25"
android:angle="270" />
<stroke
android:width="3dp"
android:color="@color/grey05" />
<corners
android:radius="3dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />
</shape>
</item>
</selector>
button.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);
will give you a red shaded button,.
button.getBackground().setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);
I'd like to change the color of a standard Android button slightly in order to better match a client's branding. For example, see the "Find a Table" button for the OpenTable application: The best way I've found to do this so far is to change the Button's drawable to the following drawable located in res/drawable/red_button.xml: But doing that requires that I actually create three different drawables for each button I want to customize (one for the button at rest, one when focused, and one when pressed). That seems more complicated and non-DRY than I need. All I really want to do is apply some sort of color transform to the button. Is there an easier way to go about changing a button's color than I'm doing? android layout drawable |
protected by Will♦ Sep 22 '10 at 13:19
This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have more than 10 reputation.
4 Answers
Mike, you might be interested in color filters. You can play with them to achieve the color you want. An example: button.getBackground().setColorFilter(new LightingColorFilter(0xFFFFFFFF, 0xFFAA0000)); |
Nope, I think you have the right answer there. Be grateful you only need three states -- there are other possible ones (e.g., disabled, selected). You can certainly just set the background to be a solid color, which is simpler, but it means you won't see focus, clicks, etc. |
=================================
=================================
=================================
'스마트기기개발관련 > 안드로이드 개발' 카테고리의 다른 글
안드로이드 탭 구현 관련 (0) | 2020.09.19 |
---|---|
주요 안드로이드 기기 개발 해상도, DIP, DP, DPI, DENSITY 정리 관련 (0) | 2020.09.19 |
안드로이드 나만의 Seekbar 만들기 (0) | 2020.09.19 |
안드로이드 여러해상도 지원, DIP 관련 (0) | 2020.09.19 |
안드로이드 해상도 상관없이 나만의 대화상자 만들기 (0) | 2020.09.19 |