10
A
回答
37
您想使用Android的Shape Drawables。 http://developer.android.com/guide/topics/resources/drawable-resource.html#Shape
繪製/ 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>
然後,你必須創建從這些形狀可繪製一個 「選擇」 繪製。這使您可以根據狀態使按鈕顯示不同。 IE:追問下,有重點,等等 http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
繪製/ 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>
獎勵:您可能希望創建一個樣式的按鈕,這樣你可以讓他們在整個程序是一致的。你可以減少這一步,只需設置按鈕的android:background =「@ drawable/cool_button」。
值/ styles.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyCoolButton">
<item name="android:background">@drawable/cool_button_background</item>
</style>
</resources>
最後,按鈕!
<?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>
8
導入PorterDuff和使用setColorFilter()如下
import android.graphics.PorterDuff.Mode;
Button btn = (Button) findViewById(R.id.myButton);
btn.getBackground().setColorFilter(Color.GRAY, Mode.MULTIPLY);
= 「@風格/ CoolButton」 應改爲風格
+0
這將是如果您留下解釋而不僅僅是代碼,對讀者有幫助。 – 2017-04-13 00:56:22
相關問題
- 1. Android:不改變背景顏色的圓形按鈕角落
- 2. 的Webkit按鈕:背景顏色溢出圓角
- 3. android:帶有動態背景顏色的圓角背景的TextView
- 4. Android應用背景顏色按鈕
- 5. NSAttributedString背景顏色和圓角
- 6. hotcocoa按鈕背景顏色
- 7. button.js中的按鈕背景顏色
- 8. Android的 - 按鈕的背景顏色縮小按鈕的高度
- 9. 支持Android背景顏色的圓角ImageView?
- 10. 編程設置按鈕的背景顏色去除保證金和圓角
- 11. NSAnimation刪除按鈕的背景顏色
- 12. sencha的按鈕背景顏色
- 13. 更改按鈕的背景顏色
- 14. 更改按鈕背景顏色使用if-else在android中
- 15. 更改Android應用程序中按鈕的背景顏色
- 16. 打印按鈕背景顏色
- 17. C++ Win32單選按鈕背景顏色
- 18. 按鈕背景顏色保存java
- 19. 按鈕背景顏色不起作用
- 20. 設置背景顏色改變按鈕
- 21. 整頁背景顏色角
- 22. 按鈕大小和按鈕按下背景顏色的變化
- 23. 如何更改Android上的切換按鈕的背景顏色
- 24. 的Android按鈕使用的背景顏色和漣漪效應
- 25. 在黑莓中選中時更改按鈕的背景顏色
- 26. 具有不同背景顏色的Android按鈕
- 27. 確定Android按鈕背景的默認顏色
- 28. Android不會更新UI上的按鈕背景顏色線程
- 29. 如何獲取Android上按鈕的背景顏色?
- 30. 刪除圓角麪包屑的左右背景顏色
小幅盤整風格= 「@風格/ MyCoolButton」 – user2582651 2017-03-04 06:28:28