Вывод:
![enter image description here](https://i.stack.imgur.com/9qU7v.png)
Поместите этот spinner_item.xml
в свою layout
папку, которая определяет ваши Spinner
каждые item
s textColor
, textSize
, paddings
et c.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="0dp"
android:paddingEnd="0dp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:gravity="center"
android:textSize="14sp"
android:textColor="#ffffff" />
Поместите этот activity_main.xml
в свою layout
папку, которая является основным файлом макета.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="#F81616"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="16dp"
android:text="TextView"
android:textColor="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/divider1"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="@+id/divider1"
app:layout_constraintTop_toBottomOf="@+id/divider1"
app:layout_constraintVertical_bias="0.0" />
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:backgroundTint="#ffffff"
android:spinnerMode="dropdown"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.32" />
<View
android:id="@+id/divider1"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="#ffffff"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/spinner"
app:layout_constraintStart_toStartOf="@+id/spinner"
app:layout_constraintTop_toBottomOf="@+id/spinner"
app:layout_constraintVertical_bias="0.0" />
<View
android:id="@+id/divider2"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/textView"
app:layout_constraintStart_toStartOf="@+id/textView"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
Затем в файле MainActivity.java
добавьте следующий код:
Kotlin:
val spinnerArray = arrayOf("Spinner", "Category-1", "Category-2", "Category-3")
val spinnerArrayAdapter = ArrayAdapter(this, R.layout.spinner_item, spinnerArray)
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = spinnerArrayAdapter
Java:
Spinner spinner = findViewById(R.id.spinner);
String[] spinnerArray = {"Spinner", "Category-1", "Category-2", "Category-3"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter(this, R.layout.spinner_item, spinnerArray);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerArrayAdapter);