2013-08-06 107 views

回答

12

基本上你需要創建一些新的XML文件並將它們應用到你的Button元素。正如我從模型中可以看到的那樣,您需要中風和背景色以及應用的一些陰影效果,您可以更多地研究陰影,但背景顏色和筆劃非常簡單。

下面是一個例子,done_rounded_btn.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:state_pressed="true" 
     android:state_enabled="true" 
     android:drawable="@drawable/zzzzzzzzz_btn_orange" /> 
    <item 
     android:state_focused="true" 
     android:state_enabled="true" 
     android:drawable="@drawable/zzzzzzzzz_btn_orange" /> 
    <item 
     android:state_focused="false" 
     android:state_enabled="false" 
     android:drawable="@drawable/zzzzzzzzz_btn_inactiv" /> 
    <item android:drawable="@drawable/zzzzzzzzz_btn_black"/> 
</selector> 

的選擇部分,然後創建對應的實體模型可繪製自定義。

一個例子,zzzzzzzzzz_btn_orange:

<shape xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shape="rectangle" > 

    <solid 
     android:color="@color/done_color"> 
    </solid> 

    <corners 
     android:bottomLeftRadius="3dp" 
     android:bottomRightRadius="3dp" 
     android:topLeftRadius="3dp" 
     android:topRightRadius="3dp" /> 

</shape> 

然後將其添加到您的按鈕爲背景,main.xml中:

<Button 
      android:id="@+id/registers_btn" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerVertical="true" 
      android:layout_marginBottom="10dp" 
      android:layout_marginLeft="15dp" 
      android:layout_marginTop="10dp" 
      android:background="@drawable/done_rounded_btn" 
      android:text="@string/done_txt" 
      android:textColor="@color/white" 
      android:textSize="15sp" /> 

希望這有助於!

+0

我有兩個這個方向的PNG。那麼,我該如何旋轉(傾斜)按鈕。 – Darshak

+0

好吧,您將從頭開始重新創建PNG的效果(至少這是我會做的)。使用顏色選擇器(比如Mozilla上的加載項)來獲取每個按鈕狀態的顏色代碼,將其添加到繪圖中,然後添加筆觸效果。我不確定陰影效果,但是您可以在StackOverflow上查看此處的內容 –

+3

所有z都有什麼? –

5

可以在XML使用它來代替標準按鈕,設置選擇爲背景:

import android.content.Context; 
import android.graphics.Bitmap; 
import android.graphics.Color; 
import android.util.AttributeSet; 
import android.view.MotionEvent; 
import android.widget.Button; 

/** 
* Custom Shape Button which ignores touches on transparent background. 
*/ 
public class ButtonWithUntouchableTransparentBg extends Button { 

    public ButtonWithUntouchableTransparentBg(Context context) { 
     this(context, null); 
    } 

    public ButtonWithUntouchableTransparentBg(Context context, AttributeSet attrs) { 
     this(context, attrs, 0); 
    } 

    public ButtonWithUntouchableTransparentBg(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setDrawingCacheEnabled(true); 
    } 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     int x = (int) event.getX(); 
     int y = (int) event.getY(); 

     // ignores touches on transparent background 
     if (isPixelTransparent(x, y)) 
      return true; 
     else 
      return super.onTouchEvent(event); 
    } 

    /** 
    * @return true if pixel from (x,y) is transparent 
    */ 
    private boolean isPixelTransparent(int x, int y) { 
     Bitmap bmp = Bitmap.createBitmap(getDrawingCache()); 
     int color = Color.TRANSPARENT; 
     try { 
      color = bmp.getPixel(x, y); 
     } catch (IllegalArgumentException e) { 
      // x or y exceed the bitmap's bounds. 
      // Reverts the View's internal state from a previously set "pressed" state. 
      setPressed(false); 
     } 

     // Ignores touches on transparent background. 
     if (color == Color.TRANSPARENT) 
      return true; 
     else 
      return false; 
    } 
} 
+0

其顯然,但我不得不說,這個解決方案支持選擇器,所以只需添加幾個按鈕的狀態圖像(使用selector.xml)和它的準備好2go。 – Stan

-1

內您的項目把外形在選擇XML

EX從我的代碼:

<!-- if pressed --> 
<item android:state_pressed="true"><shape android:padding="10dp" android:shape="rectangle"> 
     <solid android:color="@color/blue" /> 

     <corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" /> 
    </shape></item> 

<!-- if not pressed --> 
<item><shape android:padding="10dp" android:shape="rectangle"> 
     <solid android:color="@color/Purbble" /> 

     <corners android:bottomLeftRadius="10dp" android:bottomRightRadius="10dp" android:topLeftRadius="10dp" android:topRightRadius="10dp" /> 
    </shape></item> 

+0

需要定義顏色屬性 – Amt87

6

您還可以創建一個使用內部選擇器的形狀。如果你的形狀只是在不同的狀態下改變它的顏色,這是一個更清潔。

顏色/ color_selector.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:color="@color/blue_dark" android:state_pressed="true" /> 
    <item android:color="@color/blue_light" /> 
</selector> 

抽拉/形狀。XML

<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="@color/color_selector" /> 
    <corners android:bottomLeftRadius="6dip" android:bottomRightRadius="6dp" /> 
    <padding android:bottom="0dip" android:left="0dip" android:right="0dip" android:top="0dip" /> 
</shape>