问题
I have a spinner and i have an arraylist having the names of the states which I am setting to the spinner using array adapter.I think there is some issue with the layout of the dropdown_item of the spinner and it is taking the style of the app. Since i have used material style in my project, the same style is getting applied here.
The items are visible the items are selectable also, but the dropdown list is not being displayed properly
Can anybody please help me out.
This is how im setting my arraylist to the spinners.
mArrayAdapter_userType = new ArrayAdapter<String>(RegistrationActivity.this, R.layout.spinner_text, aArrayList_userType);
mArrayAdapter_userType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner_userType.setAdapter(mArrayAdapter_userType);
回答1:
Add this dependencies in build.gradle
file
compile 'com.github.rey5137:material:1.2.2'
In Xml write this code.
<com.rey.material.widget.Spinner
android:id="@+id/spinner_label"
style="@style/LightSpinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:minWidth="128dp"
android:padding="8dp"
app:spn_label="Spinner with arrow" />
In Java class write this code.
Spinner spn_label = (Spinner) findViewById(R.id.spinner_label);
String[] items = new String[20];
for (int i = 0; i < items.length; i++) {
items[i] = "Item " + String.valueOf(i + 1);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.row_span, items);
spn_label.setAdapter(adapter);
Create row_span.xml in your layout
folder.
<?xml version="1.0" encoding="utf-8"?>
<com.rey.material.widget.TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/row_spn_tv"
android:layout_width="match_parent"
android:layout_height="48dp"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textDirection="locale"
style="@style/LightSpinner"/>
When you run the below output is generated.
for more detail visit this : https://github.com/rey5137/material
Without using Any Library :
Write this in your xml
<Spinner
android:id="@+id/planets_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Create Array data in your resources file
<?xml version="1.0" encoding="utf-8"?>
<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>
</resources>
And use this in Java
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);
来源:https://stackoverflow.com/questions/37608108/android-spinner-showing-transparent-screen-with-distortion-of-data-but-items-are