如何在 Android 中使 ListView 透明?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1377336/
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-20 03:00:57  来源:igfitidea点击:

How to make a ListView transparent in Android?

androidandroid-layoutandroid-listview

提问by Tamaghna M

How to make the ListView transparent in android?

如何在android中使ListView透明?

The background android screen image should be visible.

背景 android 屏幕图像应该是可见的。

回答by Matthias

You should use the more verbose

你应该使用更详细的

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

Updated with Hymany's response. I originally merely meant to add to his answer, since he was using a plain hex color in the sample.

更新了 Hymany 的回复。我最初只是想添加到他的答案中,因为他在示例中使用了普通的十六进制颜色。

回答by Hymany

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

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

回答by bhatt4982

  • How to make the ListView transparent in android?
  • 如何在android中使ListView透明?

As Hymany mentioned, setting attributes for list view will do the job.

正如 Hymany 所提到的,为列表视图设置属性将完成这项工作。

android:background="#00000000" 
android:cacheColorHint="#00000000"
  • The background android screen image should be visible.
  • 背景 android 屏幕图像应该是可见的。

In Android manifest file add following attribute to activity.

在 Android 清单文件中,向活动添加以下属性。

android:theme="@android:style/Theme.Dialog"

回答by nikki

try this:

尝试这个:

list.setCacheColorHint(Color.TRANSPARENT);

回答by Denis

Add this to make list items stay transparent when pressed:

添加此项以使列表项在按下时保持透明:

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

回答by cornbread ninja

This article helps explain the nuances of ListView in conjunction with a custom background - http://developer.android.com/resources/articles/listview-backgrounds.html

本文有助于解释 ListView 与自定义背景的细微差别 - http://developer.android.com/resources/articles/listview-backgrounds.html

tl;dr - put this in the offending ListView's xml somewhere:

tl;dr - 把它放在有问题的 ListView xml 的某个地方:

android:cacheColorHint="#00000000"

android:cacheColorHint="#00000000"

回答by Aaron

If you want to use partial transparency, this would help you while setting your color codes.

如果您想使用部分透明度,这将有助于您设置颜色代码。

2 hex characters can be appended to any hex color code. The first 2 characters in an 8-digit hex color code represents its opacity in Android.

2 个十六进制字符可以附加到任何十六进制颜色代码。8 位十六进制颜色代码中的前 2 个字符表示其在 Android 中的不透明度。

The 2 hex characters can range from 00 to FF. For example-

2 个十六进制字符的范围可以从 00 到 FF。例如-

  • Normal opaque black hex- "#000000"
  • Fully transparent black- "#00000000"
  • Fully opaque black- "#FF000000"
  • 50% transparent black- "#80000000"
  • 正常不透明黑色十六进制-“#000000”
  • 全透明黑色-“#00000000”
  • 完全不透明的黑色-“#FF000000”
  • 50% 透明黑色-“#80000000”

This way you can change any color to any level of transparency.

通过这种方式,您可以将任何颜色更改为任何级别的透明度。

Source- http://zaman91.wordpress.com/2010/03/22/android-how-to-create-transparent-or-opeque-background/

来源- http://zaman91.wordpress.com/2010/03/22/android-how-to-create-transparent-or-opeque-background/

回答by Kailas

You can use these

你可以使用这些

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

回答by romanzah

try this:

尝试这个:

android:cacheColorHint="@null"

回答by XMight

The answers above will work, but there is a chance that when you'll scroll the listView, it will darken, like in this case: android-listview problem with transparent cells

上面的答案会起作用,但有可能当你滚动 listView 时,它会变暗,就像在这种情况下:android-listview problem with transparent cells

To solve the issue, you can use the cacheColorHint as mentioned above, but if you add the ListView dynamically (from code, not xml), then this will not work. You are forced to declare the ListView in XML, dunno if this is a bug or something else.

为了解决这个问题,你可以使用上面提到的 cacheColorHint,但是如果你动态添加 ListView(从代码,而不是 xml),那么这将不起作用。您被迫在 XML 中声明 ListView,不知道这是错误还是其他原因。