Java Android 使视图透明,或至少半透明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18553603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 08:59:46  来源:igfitidea点击:

Android make a view transparent, or at least semi transparent

javaandroid

提问by Akshat

I'm using the AndroidSlidingUpPanel. I'm trying to make one of the views semi-transparent. So that you can see what's under it. So its basically a LinearLayout which moves, but I can't see whats underneath.

我正在使用AndroidSlidingUpPanel。我正在尝试使其中一个视图半透明。这样你就可以看到它下面的东西了。所以它基本上是一个移动的 LinearLayout,但我看不到下面是什么。

I've tried setting the alpha, the background color (with alpha)

我试过设置alpha背景颜色(带 alpha)

xml settings:

xml设置:

android:background="@android:color/transparent"

as well as:

也:

android:background="#22000000"

I've also tried programmatically:

我也以编程方式尝试过:

view.setAlpha((float) 0.45));

The thing is everything I do doesn't make it transparent. I can see the colours change but its still fully opaque.

问题是我所做的一切并没有使它透明。我可以看到颜色发生变化,但它仍然完全不透明。

Perhaps i've missed something? How can I make it transparent so I can see whats underneath

也许我错过了什么?我怎样才能让它透明,这样我才能看到下面是什么

回答by Android Stack

try to refer it to drawable shape as bellow :

尝试将其引用为可绘制形状,如下所示:

android:background="@drawable/transparant_layout"

android:background="@drawable/transparant_layout"

transparant_layout:

透明布局:

<?xml version="1.0" encoding="utf-8" ?> 
 <shape xmlns:android="http://schemas.android.com/apk/res/android">
   <solid android:color="#00000000" /> 
 </shape>

Hope this help.

希望这有帮助。

回答by Tushar

The color hex code is built like this. #ARGB or for a more fine grained control #AARRGGBB which means AlphaRedGreenBlue. Try some colour range for the transparency . the problem is with your hex code for color

颜色十六进制代码是这样构建的。#ARGB 或更细粒度的控制 #AARRGGBB,这意味着 AlphaRedGreenBlue。尝试一些颜色范围的透明度。问题在于您的颜色十六进制代码

回答by jyomin

In XML set Background attribute to any colour White(#FFFFFF) shade or Black(#000000) shade.if you want transparancy just put 80 before the actual hash code.

在 XML 中,将 Background 属性设置为任何颜色 White(#FFFFFF) 阴影或 Black(#000000) 阴影。如果您想要透明度,只需在实际哈希代码之前放置 80。

#80000000   

回答by Franco

You need to add slidingpanel:overlay="true"to SlidingUpPanelLayout, and then use the color on the transparent view as you decribed (android:background="@android:color/transparent"or whatever level of transparency you want).

您需要添加slidingpanel:overlay="true"SlidingUpPanelLayout,然后使用您描述的透明视图上的颜色(android:background="@android:color/transparent"或您想要的任何透明度级别)。

回答by Ross

Use android:alpha="0.1" to set opacity of a layout

使用 android:alpha="0.1" 设置布局的不透明度

<FrameLayout
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:alpha="0.2"
    android:background="#000000"

https://developer.android.com/reference/android/view/View.html#setAlpha(float)

https://developer.android.com/reference/android/view/View.html#setAlpha(float)