[英]kotlin - set aplha for views programmatically
I want to change alpha value of my views programmatically , it can easily be set via xml and it works fine , the value is between 0 to 1 and if I set it 0.5 in xml it semi transparent the view in my xml layout .我想以编程方式更改我的视图的 alpha 值,它可以通过 xml 轻松设置并且工作正常,该值介于 0 到 1 之间,如果我在 xml 中将其设置为 0.5,则它使我的 xml 布局中的视图半透明。
but how can I set it in kotlin ?但是我如何在 kotlin 中设置它?
I've tried this code but it doesn't work at all :我试过这段代码,但它根本不起作用:
ln.alpha = 0.5f
i've also tried to set value by integer ,someone said that it gets the value between 0 to 255 but when I set it , it shows me an error that the value is <=1我也试过用整数设置值,有人说它得到的值在 0 到 255 之间,但是当我设置它时,它显示一个错误,该值是 <=1
how can I dos o ?我怎么办?
There are two types of alpha
you can set您可以设置两种类型的alpha
Sets the opacity of the view to a value from 0 to 1, where 0 means the view is completely transparent and 1 means the view is completely opaque.将视图的不透明度设置为 0 到 1 之间的值,其中 0 表示视图完全透明,1 表示视图完全不透明。
view.alpha = 0.5f
Specify an alpha value for the drawable.为 drawable 指定一个 alpha 值。 0 means fully transparent, and 255 means fully opaque. 0 表示完全透明,255 表示完全不透明。
view.background.alpha = 128
Use this:用这个:
open fun setAlpha(alpha: Float): Unit
Example:例子:
view.setAlpha(0.6f);
Read more at: https://developer.android.com/reference/kotlin/android/view/View.html#setAlpha(kotlin.Float)阅读更多信息: https : //developer.android.com/reference/kotlin/android/view/View.html#setAlpha(kotlin.Float)
use this one使用这个
views.setAlpha(0.5f); // for 50%
and if it is not worked use this one如果它不起作用,请使用这个
views.getBackground().setAlpha(128);// for 50% and 256 for 100%
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.