【问题标题】:How to get rid of white borders in spinnerDropDownItemStyle when using a custom Spinner?使用自定义 Spinner 时如何摆脱 spinnerDropDownItemStyle 中的白色边框?
【发布时间】:2013-07-06 10:53:58
【问题描述】:

我创建了一个客户微调器,当弹出窗口出现时不要去掉空格:

这就是我创建自定义微调器的方式:
我将 simple_spinner_dropdown_item.xmlsimple_spinner_item.xml 从安装目录复制到我的布局文件夹并更改了样式属性,以便它们指向我自己的样式:

simple_spinner_item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    ...
    style="@style/spinnerItemStyle" />

simple_spinner_dropdown_item.xml

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android" 
...
style="@style/spinnerDropDownItemStyle" />

spinnerItemStyle.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="spinnerItemStyle" parent="android:Widget.TextView.SpinnerItem">
        <item name="android:textColor">#FFF</item>
        <item name="android:textSize">12sp</item>
        <item name="android:background">@null</item>
    </style>
</resources>

spinnerDropDownItemStyle.xml

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="spinnerDropDownItemStyle" parent="android:TextAppearance.Widget.DropDownItem">
        <item name="android:textColor">#FFF</item>
        <item name="android:padding">20dp</item>
        <item name="android:textSize">12sp</item>
        <item name="android:background">@drawable/spinner_rectangle</item>
    </style>
</resources>

spinner_rectangle.xml

<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="#333" />
    <stroke
        android:width="0dip" />
</shape>

微调器只是在 Activity 的布局文件中引用:

        <Spinner 
            android:id="@+id/preferences_spinner_language"
            android:background="@drawable/spinner_dropdown" />

而且我不使用自定义适配器,只使用通常的 ArrayAdapter,我使用上面的资源和 Android 资源:

Spinner spinner = (Spinner)findViewById(R.id.preferences_spinner_language);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.preferences_language_array, R.layout.simple_spinner_item);
adapter.setDropDownViewResource(R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

我猜有一些底层 ImageView 会产生这些白色边框。我怎样才能访问它并摆脱这些边框或为它们着色?我更喜欢不需要创建自己的 ArrayAdapter 的解决方案。

【问题讨论】:

  • 希望对您有所帮助! stackoverflow.com/questions/10916658/…
  • 很遗憾没有,因为 Android 2.3.3 中没有 Theme.Holo。我使用的父主题中不存在android:dropDownListViewStyle。由于其他原因,我已经创建了自己的主题,并且源自parent="android:Theme"

标签: android customization android-spinner


【解决方案1】:

在您的主题中,覆盖 android:dropDownListViewStyle 样式。例如:

<style name="AppTheme" parent="@android:style/Theme.Holo">
    <item name="android:dropDownListViewStyle">@style/MySpinnerStyle</item>
</style>

<style name="MySpinnerStyle" parent="android:style/Widget.ListView.DropDown">
    <item name="android:divider">#FFEEEEEE</item>
    <item name="android:dividerHeight">1dp</item>
</style>

【讨论】:

  • 我不知道这应该如何工作。我已经覆盖了我的主题,因为我为我的整个应用程序使用了自定义主题(parent="android:Theme")。在我当前的主题中,我没有像 android:dropDownListViewStyle 这样的项目(Eclipse 的内容辅助没有显示这样的建议)。这就是为什么所有其他有类似问题的线程对我没有帮助。
  • 如果你的父主题是android:Theme,这个值已经为你设置好了。您是否尝试将 android:dropDownListViewStyle 标签添加到您的自定义主题中?
  • 我尝试使用自己的主题作为 parent 或 parent="android:Theme",因为 Android 2.3.3 中没有 Theme.Holo,但分隔符保持白色。 (尽管我在 AndroidManifest 中将自定义主题更改为派生主题)。我尝试将 &lt;item name="android:dropDownListViewStyle"&gt;@style/MySpinnerStyle&lt;/item&gt; 及其声明添加到我的自定义主题中,但仍然没有变化。
  • 我知道已经有好几年了,但是你解决了这个问题吗?我遇到了完全相同的问题,我已经尝试了所有方法,但没有运气:/
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多