回答
创建drawable
一个red_button.xml
这样的:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke android:width="2dp"
android:color="#80FFFFFF" />
<corners android:radius="25dp" />
<gradient android:angle="270"
android:centerColor="#ff0000"
android:endColor="#ff0000"
android:startColor="#ff0000" />
</shape>
要获得相同的形状作为默认Button
可以玩弄radius
和stroke width
和stroke color
让你Button
你想要的。
编辑:您可以将颜色添加到原来的默认Button
这样的:
Drawable d = yourButton.getBackground();
PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP); // or whatever color you like
d.setColorFilter(filter);
我可以通过代码执行此操作吗,而不是将它放入XML中? – 2014-12-02 15:39:54
为什么你想在代码中做到这一点?创建XML,只需在button.setBackgroundResource(R.drawable.red_button)中添加背景代码; – Carnal 2014-12-02 15:41:51
我想通过代码来做到这一点,因为我通过代码设置背景颜色。如果我制作了一个通用的按钮布局,并将其设置为setBackgroundResource(),我可以使用代码更改其背景颜色吗?所以我不需要为每种颜色需要一个布局? – 2014-12-02 16:38:52
你可能已经错过了布局设置保证金。实际上,按钮周围有一个空白区域,所有按钮都会被触摸,所以当您设置背景时,空白区域也会变成红色。我猜这应该没问题。
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/one"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="About"
android:layout_margin="5dp" />
<Button
android:id="@+id/two"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Gallery"
android:layout_margin="5dp"/>
<Button
android:id="@+id/third"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Media"
android:layout_margin="5dp"/>
</LinearLayout>
在strings.xml中
<color name="red">#FF0000</color>
在Java
one = (Button) findViewById(R.id.one);
two = (Button) findViewById(R.id.two);
three = (Button) findViewById(R.id.third);
one.setBackgroundResource(R.color.red);
two.setBackgroundResource(R.color.red);
three.setBackgroundResource(R.color.red);
输出
- 1. 更改空白背景颜色
- 2. C#更改按钮的背景颜色
- 3. 更改按钮的背景颜色
- 4. 更改按钮颜色作为背景颜色更改?
- 5. jqplot背景颜色区域?
- 6. 更改按钮的背景颜色与我选择的颜色
- 7. 更改背景颜色/按钮颜色在TKinter
- 8. 如何更改qtabbar空白空间的背景颜色pyqt
- 9. Xcode - 更改按钮上的按钮背景颜色点击
- 10. 编辑/完成按钮,更改完成按钮背景颜色
- 11. 更改UIActionSheet按钮背景颜色和按钮字体
- 12. NSAnimation删除按钮的背景颜色
- 13. 设置背景颜色改变按钮
- 14. 更改编辑区背景颜色
- 15. UWP Template10 - 更改主题时更改按钮背景颜色
- 16. hotcocoa按钮背景颜色
- 17. 按钮背景颜色
- 18. iOS 7 - 我可以更改UITableViewCell中的删除按钮背景颜色吗?
- 19. 按下时更改操作栏按钮的背景颜色
- 20. 如何动态更改按下按钮的背景颜色?
- 21. 当按下按钮时更改背景的行颜色
- 22. 更改按钮上的listview背景颜色按
- 23. 按GLUT中的按钮更改背景颜色
- 24. 更改WPF DatePicker年/月标题区域背景颜色
- 25. 如何更改悬停时链接背景颜色的区域
- 26. 如何更改eclipse中未定义区域的背景颜色?
- 27. 使用jQuery更改文本区域的背景颜色Spotfire
- 28. 将背景颜色更改为视图的有限区域 - Android
- 29. CSS - 按钮的背景颜色不改变移动
- 30. 更改背景颜色3
检查我编辑的答案。 – Carnal 2014-12-03 10:11:32