简体   繁体   English

Spinner功能在Android 6.0.1上无效

[英]Spinner functionality not working on Android 6.0.1

I am using Spinner for displaying some values. 我正在使用Spinner来显示一些值。 And the strange issue is that 而奇怪的问题是

The dropdown is displaying correctly, but when I select any item from dropdown, is not displayed in the box. 下拉列表显示正确,但是当我从下拉列表中选择任何项目时,框中不会显示。

And the strange thing is this functionality is working on all Android Operating Systems before 6.0.1(ie 6.0.0 and previous) . 奇怪的是,此功能适用于before 6.0.1(ie 6.0.0 and previous)所有Android操作系统before 6.0.1(ie 6.0.0 and previous) I have also tried AppCompatSpinner and the result is same. 我也试过AppCompatSpinner ,结果是一样的。

main.xml: main.xml中:

<Spinner
    android:id="@+id/spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="0.3"
    android:entries="@array/values" />

Main.java: Main.java:

Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setSelection(5); // Not displaying 5th item, Yes! there are more than 5 items.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        spinner.setSelection(position);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        spinner.setSelection(5);
    }
});

It's weird that I am answering my own question. 我回答自己的问题很奇怪。 But after doing lots of research finally I found a solution. 但经过大量研究后我终于找到了解决方案。

Solution: 解:

There is nothing wrong in the code that I previously wrote. 我之前写的代码没有错。 It's just issue of inner padding in Android OS 6.0.1 这只是Android OS 6.0.1中内部填充的问题

In the release of Android OS 6.0.1 they made some changes in inner padding of a Spinner . Android OS 6.0.1的发布中,他们对Spinner内部填充进行了一些更改。

After reading this related question on SO I adjusted my Spinner width and made it visible in all the Android OS versions. 在SO上阅读了这个相关的问题之后 ,我调整了我的Spinner宽度并使其在所有Android OS版本中都可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM