Android按钮背景
我试图在我的应用程序中设置按钮的背景色,但无法实现所需的结果...
我尝试设置的颜色是LightingColorFilter。为此,我使用holo_green_light
我得到的颜色不是LightingColorFilter,而是浅灰色和holo_green_light的混合。
我尝试使用LightingColorFilter并没有成功。
有没有一种方法可以通过编程来实现,因此按钮看起来像按钮,而不是具有我所需颜色的平面矩形。
9个解决方案
41 votes
这是我做用不同的颜色的习惯按钮的方式。
android:color="#80FFFFFF" />
android:centerColor="#90150517"
android:endColor="#90150517"
android:startColor="#90150517" />
这样,您将其设置为背景。
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:layout_marginBottom="25dp"
android:layout_centerInParent="true"
android:background="@drawable/button"/>
Bebin T.N answered 2020-02-09T21:37:49Z
24 votes
如果您想保留常规样式(圆角等)并且仅更改背景颜色,则可以使用backgroundTint属性
android:backgroundTint="@android:color/holo_green_light"
Mark Proctor answered 2020-02-09T21:38:09Z
18 votes
按钮比仅更改一种颜色更为复杂。
Android按钮是使用许多9补丁和状态可绘制对象制作的。 您应该查看一下Android Holo Colors来生成这些颜色,您需要选择“颜色按钮”,它将为您生成必要的图像和xml。
Tonithy answered 2020-02-09T21:37:25Z
16 votes
如果您不介意对其进行硬编码,则可以执行〜> android:background =“#eeeeee”并删除您希望的任何十六进制颜色。
看起来像这样...
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:text="@string/ClickMe"
android:background="#fff"/>
V.Nice answered 2020-02-09T21:38:34Z
13 votes
创建具有以下内容的/res/drawable/button.xml:
android:shape="rectangle" android:padding="10dp">
android:bottomRightRadius="3dp"
android:bottomLeftRadius="3dp"
android:topLeftRadius="3dp"
android:topRightRadius="3dp"/>
然后,您可以使用以下命令:
android:id="@+id/button_save_prefs"
android:text="@string/save"
android:background="@drawable/button"/>
Giant answered 2020-02-09T21:38:58Z
5 votes
不必成为那个铁杆。
尝试这个 :
YourButtonObject.setBackground(0xff99cc00);
Mehdi answered 2020-02-09T21:39:22Z
4 votes
为什么不只使用setBackgroundColor(getResources().getColor(R.color.holo_light_green))?
编辑:如果您想要一个看起来更像Android按钮的东西,则需要创建一个渐变并将其设置为背景。 例如,您可以查看此问题。
Jonathan answered 2020-02-09T21:39:47Z
0 votes
为了保持样式,请使用:
int color = Color.parseColor("#99cc00");
button.getBackground().mutate().setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC));
BluEOS answered 2020-02-09T21:40:06Z
0 votes
除了Mark Proctor的答案:
如果要保留默认样式,但按钮上需要有条件的颜色,只需设置backgroundTint属性即可,如下所示:
android:backgroundTint="@drawable/styles_mybutton"
创建关联文件/res/drawable/styles_mybutton.xml,然后使用以下模板并根据您的喜好更改颜色:
android:color="@android:color/white">
android:color="#cfc">
LePatay answered 2020-02-09T21:40:36Z