настройка счетчика для другого вида - PullRequest
0 голосов
/ 09 мая 2020

У меня есть код xml для счетчика и текстовое представление вроде этого:

<Spinner
        android:id="@+id/spinner"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:entries="@array/EventOptions"
        android:gravity="center"
        android:textAlignment="center"
        android:textColor="@color/colorPrimaryComplement"
        style="@style/loginEditTextStyle"
        app:layout_constraintWidth_percent=".8"
        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.6" />

    <AutoCompleteTextView
        android:id="@+id/emailField2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:fontFamily="@font/montserrat"
        android:gravity="center"
        android:hint="TextView"
        android:inputType="textEmailAddress"
        android:textColor="@color/colorPrimaryComplement"
        android:textColorHint="@color/colorPrimaryComplementSemiTransp"
        android:textCursorDrawable="@null"
        android:theme="@style/loginEditTextStyle"
        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"
        app:layout_constraintWidth_percent=".8" />  

И текущий результат:
enter image description here

Но я бы хотел настроить спиннер для другого вида. Как видите, текст textView не совпадает с текстом счетчика. Итак, я хотел бы избавиться от стрелки или хотя бы выровнять текст с textView. Примерно так:

enter image description here Кроме того, я бы хотел, чтобы для каждого элемента в списке из раскрывающегося списка в счетчике textColor был белый цвет с подчеркиванием, если это возможно.

1 Ответ

0 голосов
/ 09 мая 2020

Вывод:

enter image description here

Поместите этот 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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...