【问题标题】:How can I replace the icon arrow of spinner in Android with an image?如何用图像替换Android中微调器的图标箭头?
【发布时间】:2016-12-10 08:16:34
【问题描述】:

我想用自定义图像更改 Android 中的默认微调器图标箭头。

我制作了自定义微调器(基于此答案:How to customize a Spinner in Android,只是更改了自定义值)并且效果很好,但我无法在样式或编程上更改微调器的箭头。

我看到了这个问题:How to change image of a spinner,但由于我没有Spinner 标签,因此我无法添加属性android:spinnerSelector

如何将默认箭头图标替换为图片?

提前致谢!

【问题讨论】:

    标签: android


    【解决方案1】:

    简短示例:

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this,
            R.layout.custom_spinner,
            ITEMS
    );
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    

    然后为spinner创建一个自定义xml(命名为:custom_spinner.xml):

    <?xml version="1.0" encoding="utf-8"?>
    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@android:id/text1"
        style="?android:attr/spinnerItemStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:singleLine="true"
        android:textSize="yourTextsize"
        android:drawablePadding="paddingValueYouWant"
        android:drawableRight="@drawable/ic_arrow_down" <!--your custom icon-->
        />
    

    更多自定义微调器,请关注这篇文章:How to set font custom font to Spinner text programmatically?

    【讨论】:

    • 好的,我可以使用android:drawableRight 添加自定义下拉箭头,但现在它同时显示了默认图标箭头和我添加的新图像图标。我怎样才能只显示第二个?
    • 你可以给微调器的背景android:background="@drawable/bg"android:background="@null",它应该是不可见的:)
    • 当您输入@drawable/bg 时,您的意思是使用自定义背景吗?我尝试了android:background="@null",但它并没有改变任何东西。
    • 哦,对不起,我从来没有使用 CheckedTextview 作为微调器 :(
    • 对不起,我脑子里一片混乱,我忘记了我正在使用 TextView 和 CheckedTextview 将适配器设置为微调器,所以最后我使用了微调器:')。现在使用android:background="@null" 效果很好。非常感谢:)
    【解决方案2】:

    风格上可以直接改,

    <style name="CustomSpinnerTheme" parent="android:Widget.Spinner">
        <item name="android:background">@drawable/spinner_arrow</item>
    </style>
    

    只需使用任何你想要的可绘制图标,然后在微调器中设置主题

    <Spinner
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/CustomSpinnerTheme"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-15
      • 1970-01-01
      相关资源
      最近更新 更多