Java Android Spinner - 如何使下拉视图透明?

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

Android Spinner - How to make dropdown view transparent?

javaandroidandroid-widgetspinner

提问by iamkoa

I have a custom spinner dropdown xml file in /res/layout/:

我在 /res/layout/ 中有一个自定义微调下拉 xml 文件:

spinner_view_dropdown.xml:

<?xml version="1.0" encoding="UTF-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/spinner_item_dropdown"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/transparent"
style="@style/spinner_item_dropdown" />

I'm setting the spinner dropdown via java:

我正在通过 java 设置微调下拉菜单:

// "Spinner", aka breadcrumbs
    Spinner spin = (Spinner) findViewById(R.id.breadcrumb_dropdown);

    ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.breadcrumb, R.layout.spinner_view);

    adapter.setDropDownViewResource(R.layout.spinner_view_dropdown);

    spin.setAdapter(adapter);
    // /"Spinner"

Unfortunately, a white background still exists on the spinner popup regardless if I set the background to transparent.

不幸的是,无论我是否将背景设置为透明,微调弹出窗口中仍然存在白色背景。

How do I fix this?

我该如何解决?

采纳答案by Christopher Perry

You can override the style for the dropdown, and the dropdown item by using a Theme in your app that inherits from one of the Android themes, then override the

您可以通过在您的应用程序中使用继承自 Android 主题之一的主题来覆盖下拉列表和下拉项的样式,然后覆盖

android:dropDownSpinnerStyle, or android:spinnerDropDownItemStyle, and even the android:dropDownListViewStyleattribute of the theme, pointing to your own custom style instead of the Android style that is defined in their theme. I created a fully customized spinner this way, with a transparent button AND dropdown. I even got rid of the dropdown list dividers, and set my own spacing for the dropdown items when I built the tablet app for Fandango (take a look at the sort movies spinner on the main page of the app).

android:dropDownSpinnerStyle, 或android:spinnerDropDownItemStyle, 甚至是android:dropDownListViewStyle主题的属性,指向你自己的自定义样式,而不是他们主题中定义的 Android 样式。我以这种方式创建了一个完全自定义的微调器,带有一个透明的按钮和下拉菜单。当我为 Fandango 构建平板电脑应用程序时,我什至去掉了下拉列表分隔符,并为下拉项设置了我自己的间距(看看应用程序主页上的排序电影微调器)。

Everything in Android is customizable, you just have to know where to look. ;-)

Android 中的所有内容都是可自定义的,您只需要知道在哪里查看即可。;-)

回答by Matthias

The layout you're defining is only used for an entryof your drop-down, not the drop-down itself. So setting the background to transparent won't have any effect on its background. But even if it would, setting the background to transparent would still have no effect, because a TextView (actually I believe anyview) has a transparent background by default.

您定义的布局仅用于下拉列表的条目,而不是下拉列表本身。因此,将背景设置为透明不会对其背景产生任何影响。但即使是这样,将背景设置为透明仍然没有效果,因为 TextView(实际上我相信任何视图)默认具有透明背景。

That being said, the right question would be: can you provide a custom layout for an entry's parent view (which is probably a List)? As far as I know, the answer is no, unfortunately.

话虽如此,正确的问题是:您能否为条目的父视图(可能是列表)提供自定义布局?据我所知,不幸的是,答案是否定的。

回答by n3utrino

Try

尝试

android:cacheColorHint="#00000000" 

to get transparency.

以获得透明度。

I dont know if it works for you but there is a post http://android-developers.blogspot.com/2009/01/why-is-my-list-black-android.htmlthat explains why the moving parts of a list appear in the background color.

我不知道它是否适合你,但有一篇文章 http://android-developers.blogspot.com/2009/01/why-is-my-list-black-android.html解释了为什么 a列表以背景颜色显示。

Maybe its the same issue with your spinner.

也许它与您的微调器存在相同的问题。

回答by Paul Maidment

It's a bug in 1.5 I think, see here

我认为这是 1.5 中的一个错误,请参阅此处

http://www.symsource.com/index.php?view=article&id=418&option=com_content&format=pdf

http://www.symsource.com/index.php?view=article&id=418&option=com_content&format=pdf

Run it in a 1.6 emulator or device, does it still stay white?

在 1.6 模拟器或设备中运行它,它仍然保持白色吗?

I actually came here looking for an approach to this, I suspect this may involve manually writing to the canvas or something like that.

我实际上是来这里寻找解决方法的,我怀疑这可能涉及手动写入画布或类似的东西。

Any ideas.

有任何想法吗。

P.S. Accidentally posted when I thought I was logged in, anyone know how to get rid of the anonymous comment? Maybe an admin could fix this?

PS 以为登录的时候不小心发的,有谁知道怎么去掉匿名评论?也许管理员可以解决这个问题?

回答by キキジキ

Try setting on the spinner this:

尝试在微调器上设置:

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