2011-03-23 23 views

cevap

37

Android'in Shape Drawables'ını kullanmak istiyorsunuz. http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape

çekilebilir/cool_button_background.xml

<?xml version="1.0" encoding="utf-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle"> 
    <corners 
     android:radius="@dimen/corner_radius" /> 
    <gradient 
     android:angle="270" 
     android:startColor="@color/almost_white" 
     android:endColor="@color/somewhat_gray" 
     android:type="linear" /> 
</shape> 

Daha sonra, bu şekil drawables bir "seçici" çekilebilir yaratmak gerekirdi. Bu, duruma bağlı olarak düğmenin farklı görünmesini sağlar. IE: vb http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList

çekilebilir/cool_button.xml,

<?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/cool_inner_press_bottom" /> 
    <item android:state_focused="true" android:state_enabled="true" 
     android:state_window_focused="true" 
     android:drawable="@drawable/cool_inner_focus_bottom" /> 
    <item 
     android:drawable="@drawable/cool_button_background" /> 
</selector> 

Bonus, Pres odaklanmış: belki bunları program boyunca tutarlı olmak zorunda böylece düğme için bir stil oluşturmak istiyorum. Bu adımı kesebilir ve sadece düğmenin android: background = "@ drawable/cool_button" değerini ayarlayabilirsiniz.

değerler/styles.xml

Nihayet
<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="MyCoolButton"> 
     <item name="android:background">@drawable/cool_button_background</item> 
    </style> 
</resources> 

, düğme!

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="@drawable/appwidget_bg"> 
    <Button 
     android:id="@+id/btnAction" 
     android:layout_width="wrap_content" 
     android:layout_weight="wrap_content" 
     style="@style/CoolButton" 
     /> 
</LinearLayout> 
+0

küçük düzeltme style = "@ tarzı/MyCoolButton" – user2582651

8

İthalat PorterDuff ve

import android.graphics.PorterDuff.Mode; 

Button btn = (Button) findViewById(R.id.myButton); 
btn.getBackground().setColorFilter(Color.GRAY, Mode.MULTIPLY); 
= "@ tarzı/CoolButton" tarzı şekilde değiştirilmelidir
+0

O olacağını şöyle setColorFilter() kullanın Sadece kod yerine bir açıklama yaptıysanız okuyucular için yararlıdır. –

İlgili konular