我需要将描边颜色更改为用户定义的颜色。与国家无关
我需要从应用程序更改描边颜色。用户可以更改背景颜色,因此我还需要让他们更改按钮的笔划(轮廓)。由于它已经在可绘制对象中设置(下面的示例),我还没有找到更改此设置的方法。似乎所有其他类似的问题都只是说使用 XML 文件......但这并不能让我使它变得动态。感谢您的帮助!
我需要将描边颜色更改为用户定义的颜色。与国家无关。
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff"/>
<stroke
android:width="3dp"
android:color="@color/Dim_Gray" /> <<<<--- This is what I need to change
<padding android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"
/>
<corners android:bottomRightRadius="12dp" android:bottomLeftRadius="12dp"
android:topLeftRadius="12dp" android:topRightRadius="12dp"/>
</shape>
I need to change the stroke color from the app. The user is able to change the background color so I need to also let them change the stroke (outline) of the button. As its is already set in the drawable (sample below) I have not found a way to change this. Seems like all of the other questions like this just said to use the XML file.... but that doesnt let me make it dynamic. Thank you for any help!
I need to change the stroke color to a user defined color. Nothing to do with the state.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff"/>
<stroke
android:width="3dp"
android:color="@color/Dim_Gray" /> <<<<--- This is what I need to change
<padding android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"
/>
<corners android:bottomRightRadius="12dp" android:bottomLeftRadius="12dp"
android:topLeftRadius="12dp" android:topRightRadius="12dp"/>
</shape>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
1.如果您有像这样的“视图”的可绘制文件
那么您可以更改
a.描边颜色:
Kotlin 扩展:
b.纯色:
2。如果您有像这样的“视图”的可绘制文件,
那么您可以通过按位置获取单独的可绘制对象来更改各个项目属性。
现在更改描边或纯色:
1. If you have drawable file for a "view" like this
Then you can change
a. Stroke color :
Kotlin extension:
b. Solid color :
2. If you have drawable file for a "view" like this
Then you can change the individual item attributes by taking separate drawable objects by there positions.
now to change stroke or solid color :
我需要一种方法来更改任何
GradientDrawable
的笔划颜色,而无需知道笔划的宽度。我的目标是使用 Drawable.setTint 来实现此目的。我必须在我的shape
xml 中添加一个透明的solid
元素才能使其正常工作:在您的情况下,因为您同时拥有
solid
和描边
,那么您需要使用图层列表
,其中描边添加在实体之后(以便将其绘制在顶部)。这将允许您在实体和笔划上设置不同的颜色,而无需提前知道笔划宽度是多少:I needed a way to change the stroke color of any
GradientDrawable
without knowing the width of the stroke. My goal was to do this usingDrawable.setTint
. I had to add a transparentsolid
element to myshape
xml to get it to work:In your case, since you have both a
solid
andstroke
, then you'll need to use alayer-list
, where the stroke is added AFTER the solid (so that it's drawn on top). This will let you set different colors on the solid and the stroke without knowing in advance what the stroke width is:请查看 LayerDrawable 因为它是根据 XML 创建并在运行时使用的。
这是一个演示示例:
您可以在运行时修改它,例如:
Please look at the LayerDrawable because it created from your XML and used at runtime.
Here is a Demo Example:
You can modify it at runtime like:
使用透明 (
#00000000
) 实心创建背景(res/drawable
中的文件):将此文件设置为背景:
现在,使用
android:backgroundTint
您可以更改边框的颜色。例如:
或:
Create background (file in
res/drawable
) with transparent (#00000000
) solid:set this file as background:
and now, using
android:backgroundTint
you can change color of your border.For example:
or:
尝试使用 StateList(而不是 ColorStateList)。看一下:
http://developer.android.com/guide/topics/resources /drawable-resource.html#StateList
您还可以以编程方式创建一个
ShapeDrawable
(或示例中的RoundRectShape
),然后调用按钮的设置背景可绘制
Try using StateLists (as opposed to ColorStateList). Take a look:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
You can also create a
ShapeDrawable
(or aRoundRectShape
in your example) programmatically, and then call the button'ssetBackgroundDrawable
我在在运行时更改形状边框颜色中回答了类似的问题,
它就像一样f20k 提出的解决方案,但在我的例子中,可绘制对象是 GradientDrawable 而不是 ShapeDrawable。
看看是否有效...
I answered a similar question in Change shape border color at runtime
Its like the same solution proposed by f20k but in my case the drawable was a GradientDrawable instead of a ShapeDrawable.
see if it works...
也许他们指的是颜色状态列表,它允许您更改基于按钮是否被按下/聚焦/启用/等的颜色
Perhaps they are referring to Color State Lists which allows you to change the color based on whether the button was pressed/focused/enabled/etc