回答
基本上你需要創建一些新的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" />
希望這有助於!
我有兩個這個方向的PNG。那麼,我該如何旋轉(傾斜)按鈕。 – Darshak
好吧,您將從頭開始重新創建PNG的效果(至少這是我會做的)。使用顏色選擇器(比如Mozilla上的加載項)來獲取每個按鈕狀態的顏色代碼,將其添加到繪圖中,然後添加筆觸效果。我不確定陰影效果,但是您可以在StackOverflow上查看此處的內容 –
所有z都有什麼? –
可以在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;
}
}
其顯然,但我不得不說,這個解決方案支持選擇器,所以只需添加幾個按鈕的狀態圖像(使用selector.xml)和它的準備好2go。 – Stan
內您的項目把外形在選擇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>
您還可以創建一個使用內部選擇器的形狀。如果你的形狀只是在不同的狀態下改變它的顏色,這是一個更清潔。
顏色/ 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>
- 1. 爲按鈕創建自定義形狀
- 2. 如何創建自定義形狀的按鈕,Android的
- 3. Android - 創建自定義形狀按鈕的可能方法?
- 4. Android,按鈕 - 自定義形狀
- 5. Android:自定義形狀的按鈕
- 6. Android:自定義按鈕形狀
- 7. 如何在android中自定義形狀按鈕?
- 8. 自定義按鈕形狀
- 9. android創建自定義形狀佈局
- 10. 如何創建自定義形狀
- 11. Android的使用形狀創建按鈕
- 12. 爲UIBarButton創建自定義按鈕,選擇器拋出異常
- 13. 如何創建自定義Android單選按鈕?
- 14. 創建自定義形狀/可繪製 - 6面按鈕
- 15. Android:如何在鍵盤上創建自定義形狀鍵?
- 16. 如何使用中性按鈕創建自定義首選項?
- 17. 如何在android xml中創建自定義按鈕
- 18. 如何在Android中創建開關按鈕自定義
- 19. 如何在Android中創建自定義按鈕
- 20. 如何在Android中創建動態自定義切換按鈕?
- 21. 如何在Android Studio中創建自定義圖像按鈕?
- 22. 如何在div中使用css3創建自定義形狀?
- 23. 在RadioGroup中的自定義適配器創建單選按鈕
- 24. 自定義按鈕選擇器
- 25. 如何在html/css中自定義形狀的按鈕?
- 26. 如何使按鈕形狀在WPF中的自定義路徑?
- 27. Android創建自定義按鈕
- 28. 如何創建自定義StumbleUpon按鈕?
- 29. wpf如何創建自定義按鈕
- 30. 使用CSS創建自定義形狀
如果您正在使用API> 11,你可以使用rotateX和旋轉Y的方法來達到這種說法。 – Nepster