简体   繁体   English

Android:如何指定一个不透明度 <layer-list> ?

[英]Android: How to specify the opacity of a <layer-list>?

I want my LayerList to have an opacity of 80 so I wrote: 我希望我的LayerList的不透明度为80,所以我写道:

<layer-list
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:opacity="80">

But I get: 但我得到:

Error: Integer types not allowed (at 'opacity' with value '80')

How can I specify the opacity? 如何指定不透明度?

android:opacity supports only translucent|opaque|transparent values in layer-list. android:opacity仅支持layer-list中的半透明|不透明|透明值。

By the way what is your requirement? 顺便问一下你的要求是什么?

Updated-Answer: To set opacity/less-visible as 80%, you need to set to your view 更新 - 答案:要将不透明度/不太可见设置为80%,您需要设置为您的视图

android:alpha="0.8"

To give a translucent effect, say 50% opacity, use: 为了产生半透明效果,比如50%的不透明度,请使用:

Drawable d = new ColorDrawable(Color.BLACK);
d.setAlpha(130);
mDialog.getWindow().setBackgroundDrawable(d);

'130' can be changed (0-255) to acheive desired opacity. 可以更改“130”(0-255)以实现所需的不透明度。

I think the question asks about setting the opacity of individual items inside a <layer-list> , and not the whole image programmatically. 我认为这个问题要求在<layer-list>设置单个项目的不透明度,而不是以编程方式设置整个图像。

If you know the color already, Use a combine hex RGBA code, for eg : 如果您已经知道颜色,请使用组合十六进制RGBA代码,例如:

<solid android:color="#80FF0000" />

Sets the color to red (0xFF0000) with 0x80 opacity. 将颜色设置为红色(0xFF0000),不透明度为0x80。

Same thing applies to few other items. 同样的事情适用于其他一些项目。 eg stroke 例如中风

<stroke android:color="#6000FF00" android:width="20dp" />

This creates a 20dp thick green stroke of 0x60 opacity. 这会创建一个20dp厚的绿色笔划,其0x60不透明度。

Futher, if you want to do it pro grammatically, ie modify the alpha of a specific item and not the whole drawable, use a new resource id (android:id="@+id/...") for the item and access it . 另外,如果你想以编程方式进行,即修改特定项目的alpha而不是整个drawable,请为项目和访问使用新的资源ID(android:id =“@ + id / ...”)它。 I haven't happened to verify this part though. 我没有碰巧验证这部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM