如何为按钮创建自定义形状 [英] How to create a custom shape for a Button
本文介绍了如何为按钮创建自定义形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何为按钮创建自定义形状?我可以在许多情况下为按钮背景色设置不同的颜色。谢谢您的帮助。谢谢。
我可以将其他颜色设置为
是的,您可以在运行时进行更改。为此,您必须为每个项目分配ID,以知道要在哪里进行更改。就像我在回答代码上所做的一样。
LayerDrawable layerDrawable =(LayerDrawable)ContextCompat.getDrawable(Your_Activity.this,R.drawable。 your_shape_test);
GradientDrawablegradientDrawable =(GradientDrawable)layerDrawable.findDrawableByLayerId(R.id.id_of_drawable-one);
gradientDrawable.setColor(your_color);
How to create a custom shape for a Button? Can I set different colors to button background colors for many conditions.Any help will be appreciated.Thanks.
解决方案
How to create a custom shape for a Button?
Create a test.xml
file in drawable and add code:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/one"> //<---- id is must for color change
<shape android:shape="rectangle">
<size
android:width="150dp"
android:height="40dp" />
<solid android:color="@color/colorPrimary" />
<corners android:radius="10dp"/> //<---- remove radius if not needed
</shape>
</item>
<item
android:top="10dp"
android:bottom="-12dp"
android:left="-55dp"
android:width="75dp">
<rotate
android:fromDegrees="-47">
<shape android:shape="rectangle">
<solid android:color="#ffffff" />
</shape>
</rotate>
</item>
</layer-list>
In button:
<Button
.....
android:background="@drawable/test"
...../>
Output
Can I set different colors to button background colors for many conditions?
Yes, you can change at run-time. For that you have to assign id to each item to know where you want to make changes. As I did on my answer code.
LayerDrawable layerDrawable = (LayerDrawable) ContextCompat.getDrawable(Your_Activity.this, R.drawable.your_shape_test);
GradientDrawable gradientDrawable = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.id_of_drawable-one);
gradientDrawable.setColor(your_color);
这篇关于如何为按钮创建自定义形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文