繁体   English   中英

试图将微调器背景设置为白色

[英]Trying to set spinner background white

我试图将微调器的背景色设置为白色,并且让白色微调器位于相对布局的蓝色框上方,但是当我将微调器的背景色设置为白色时,微调器在设计视图中消失

<?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:id="@+id/container"
    android:background="#F5F8FF"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.design.widget.BottomNavigationView
        android:id="@+id/navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="0dp"
        android:layout_marginStart="0dp"
        android:background="?android:attr/windowBackground"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/navigation" />

    <RelativeLayout
        android:layout_width="384dp"
        android:layout_height="65dp"
        android:layout_marginBottom="392dp"
        android:background="#23C4C4"
        app:layout_constraintBottom_toTopOf="@+id/navigation"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent">

        <Spinner
            android:id="@+id/spinner2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:background="#FFFFFF"/>
    </RelativeLayout>


</android.support.constraint.ConstraintLayout>

您做错了,您不能仅仅通过Spinner标签设置背景,您需要创建一个单独的xml设计文件并与适配器连接。 这是示例代码。

在您的java类中执行以下操作:

List<String> listSpinner = new ArrayList<String>();
                listSpinner.add("Select A Action");
                listSpinner.add("Start");
                listSpinner.add("Pause");
                listSpinner.add("Move To QA");
                listSpinner.add("Finish");
                final ArrayAdapter a = new ArrayAdapter(getContext(),R.layout.spinner_item,listSpinner);
                a.setDropDownViewResource(R.layout.spinner_dropdown_item);
                //Setting the ArrayAdapter data on the Spinner
                Spinner spinner = getActivity().findViewById(R.id.sptaskstatus);
                spinner.setAdapter(a);

注意:Spinner_Background_item将为旋转器设置背景

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    style="?android:attr/spinnerDropDownItemStyle"
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:ellipsize="marquee"
    android:textColor="#ffffff"
    android:background="@color/black"
    android:singleLine="true" />

使用spinner_item.xml更改微调器项目的文本颜色或文本外观:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:textColor="@color/white" />

尝试这个:

您可以这样创建:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="8dp"
android:background="#FFFFFF">

<Spinner
    android:layout_width="match_parent"
    android:layout_height="50dp"
    style="@style/Widget.AppCompat.DropDownItem.Spinner"
    android:id="@+id/spinner"
    />

另外一种创建自定义微调器的方法可以帮助您

如何更改Android的微调框背景设计和颜色?

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM