java Android,微调项未显示

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

Android, spinner items not showing

javaandroidstringspinneritems

提问by user3139428

This is my code and I can not get the items add inside Spinner. I don't know what's going wrong, and unable to find any other way!

这是我的代码,我无法在 Spinner 中添加项目。我不知道出了什么问题,也找不到任何其他方法!

Java:

爪哇:

 spinner = (Spinner)getView().findViewById(R.id.spinner);

 String[] datos = getResources().getStringArray(R.array.items);

 ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_item, datos);
adaptador.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

 spinner.setAdapter(adaptador);

XML:

XML:

<Spinner
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/spinner"
        android:layout_margin="26dp"
        android:textColor="#FFF"/>

strings.xml:

字符串.xml:

<string-array name="items">
    <item >Item 1</item>
    <item >Item 2</item>
    <item >Item 3</item>
    <item >Item 4</item>
</string-array>

Thanks in advance for the help.

在此先感谢您的帮助。

回答by rafsanahmad007

your spinner text color is white: change it to other color.

您的微调文字颜色为白色:将其更改为其他颜色。

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"
    android:layout_margin="26dp"
    android:textColor="#000000"/>  //change this

also you can directly access the array resource in your layout. Like:

您也可以直接访问布局中的数组资源。喜欢:

android:entries="@array/items" //add this in your spinner layout

EDIT

编辑

you can try using a custom layout for your spinner item:

您可以尝试为微调项使用自定义布局:

<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:gravity="left"
    android:background="#000000"  //dark background
    android:text="Test"
    android:textColor="#ffffff"  //white text
    android:padding="5dp"
    />

use the layout using:

使用布局使用:

ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getActivity(),
        R.layout.spinner_item, datos);

here R.layout.spinner_itemis the spinner custom layout

R.layout.spinner_item是微调器自定义布局

回答by Moreno

In my case just added some padding in the XML file.

就我而言,只是在 XML 文件中添加了一些填充。

<Spinner android:id="@+id/spinnerMuestreo"
      android:layout_toEndOf="@+id/textViewNumMuestreo"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:padding="8dp" <!--Try adding this line. -->
/>

回答by Abhishek

Try This XML Only

仅尝试此 XML

<Spinner
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/spinner"
    android:layout_margin="26dp"
    android:textColor="@android:color/black"
    android:entries="@array/items"/>

Remove Below From Acticity

从活动中删除下面

String[] datos = getResources().getStringArray(R.array.items);

 ArrayAdapter<String> adaptador = new ArrayAdapter<String>(getActivity(),
            android.R.layout.simple_spinner_item, datos);adaptador.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

 spinner.setAdapter(adaptador);

回答by Siva krishna

<resources>
<string-array name="planets_array">
    <item>Mercury</item>
    <item>Venus</item>
    <item>Earth</item>
    <item>Mars</item>
    <item>Jupiter</item>
    <item>Saturn</item>
    <item>Uranus</item>
    <item>Neptune</item>
</string-array>

Spinner spinner = (Spinner) findViewById(R.id.spinner);
  // Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
 // Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner

 spinner.setAdapter(adapter);

on selected item

在所选项目上

 public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...

public void onItemSelected(AdapterView<?> parent, View view,
        int pos, long id) {
    // An item was selected. You can retrieve the selected item using
    // parent.getItemAtPosition(pos)
}

public void onNothingSelected(AdapterView<?> parent) {
    // Another interface callback
}
 }

before that ..

在那之前 ..

spinner.setOnItemSelectedListener(this);