Выпадающий (Spinner) не отображается в окне дизайна Android - PullRequest
0 голосов
/ 10 мая 2018

Я новичок в Android. У меня вопрос, почему спиннер не отображается в окне дизайна? Для вашей помощи я добавил код ниже

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text View!"
    android:paddingRight="100dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:id="@+id/brand"


    />


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:layout_below="@+id/color"
    android:layout_alignLeft="@+id/color"
    android:id="@+id/find_beer"

    />

<Spinner
    android:id="@+id/color"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="72dp"
    >
</Spinner>

введите описание изображения здесь

Ответы [ 4 ]

0 голосов
/ 10 мая 2018

используйте этот код

<Spinner
android:id="@+id/color"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp" ></Spinner>

потому что в spinner (выпадающий список) мы добавляем элементы во время выполнения (в java-коде), чтобы он отображался при запуске приложения. а также все изменения в коде Java, такие как изменение цвета, видимость и т. д., отображаются при запуске приложения.

, а также посетите этот https://www.javatpoint.com/android-spinner-example

0 голосов
/ 10 мая 2018

изменить тип макета, например, линейный макет с ориентацией по вертикали или относительный макет набора макетов после установки значения с использованием кода Java или XML с использованием ресурсов

<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>

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);

с использованием макета XML в тег Spinner

android:entries="@array/planets_array"
0 голосов
/ 10 мая 2018

XML-код.

<Spinner
                        android:id="@+id/spCountry"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:layout_marginLeft="25dp"
                        android:layout_centerVertical="true"
                        android:layout_gravity="center"
                        android:background="@android:color/transparent"
                        android:gravity="center"
                        android:spinnerMode="dropdown" />

Код для активности

Spinner spCountry;

spRole = (Spinner)getView().findViewById(R.id.spCountry);

String[] arRoleSpinner = {"India","USA","NWZ","SA", "WI","ENG"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, arRoleSpinner);
        adapter.setDropDownViewResource(R.layout.spinner_item);
        spRole.setAdapter(adapter);

        spRole.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                ((TextView) adapterView.getChildAt(0)).setTextColor(Color.WHITE);
                ((TextView) adapterView.getChildAt(0)).setTextSize(12);


            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
0 голосов
/ 10 мая 2018

Используемые Constraints неверны. Измените свой макет следующим образом:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <Spinner
        android:id="@+id/color"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"></Spinner>

    <Button
        android:id="@+id/find_beer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"    
        app:layout_constraintBottom_toBottomOf="@+id/color"
        app:layout_constraintStart_toEndOf="@+id/color"
        app:layout_constraintTop_toBottomOf="@+id/color" />

    <TextView
        android:id="@+id/brand"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="57dp"
        android:layout_marginRight="57dp"
        android:paddingRight="100dp"
        android:text="Text View!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />  

</android.support.constraint.ConstraintLayout>

Установка следующего атрибута:

android:layout_width="wrap_content"
android:layout_height="wrap_content"

Означает следующее:

Представление должно быть достаточно большим, чтобы вместить его содержимое

Даже если вы исправите свои ограничения, не забудьте добавить контент в Spinner, в противном случае вы просто увидите стрелку выпадающего меню на экране.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...