使用可绘制资源为微调器中的项目设置背景-java android studio

使用可绘制资源为微调器中的项目设置背景-java android studio,java,android,spinner,Java,Android,Spinner,我使用下面的代码创建了一个微调器。现在我想使用一个名为circle.xml的可绘制资源,为微调器中的每个项目设置背景,以便每个数字都显示在一个圆圈内 Spinner equationSpinner = new Spinner(this); ArrayList<String> spinnerArray = new ArrayList<String>(); spinnerArray.add(""); spinn

我使用下面的代码创建了一个微调器。现在我想使用一个名为circle.xml的可绘制资源,为微调器中的每个项目设置背景,以便每个数字都显示在一个圆圈内

        Spinner equationSpinner = new Spinner(this);

        ArrayList<String> spinnerArray = new ArrayList<String>();
        spinnerArray.add("");
        spinnerArray.add("1");
        spinnerArray.add("2");
        spinnerArray.add("3");
        spinnerArray.add("4");
        spinnerArray.add("5");

        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
        equationSpinner.setAdapter(spinnerArrayAdapter);
但是,此方法要求在strings.xml文件(R.array.equations)中设置预定义的字符串数组。我需要能够根据应用程序的状态指定要以编程方式使用的字符串数组。阵列设置完成后,我可以修改它吗

编辑2: 此外,我发现我可以设置下拉项的背景,并使用我自己的编程数组,如下所示:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/text1"
    android:layout_width="28dip"
    android:layout_height="28dip"
    android:gravity="center"
    android:textSize="20sp"
    android:background="@drawable/circle"
    android:textColor="#ff0000" />
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.equations, R.layout.spinner_item);
        adapter.setDropDownViewResource(R.layout.spinner_item);
        equationSpinner.setAdapter(adapter);
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
        spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
        equationSpinner.setAdapter(spinnerArrayAdapter);
ArrayAdapter SpinnerayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u spinner\u下拉菜单项,Spinnerary);
spinnerrayadapter.setDropDownViewResource(R.layout.spinner_项);
equationSpinner.setAdapter(SpinNearrayaAdapter);

唯一的问题是能够将资源应用于微调器本身的背景(而不是下拉项)。对于主微调器项,似乎没有与setDropDownViewResource等效的内容。

假设
textview\u background.xml
是要为
微调器的textview提供的背景

在drawable中创建一个文件,并将其命名为
textview\u background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/holo_blue_dark" />
<corners android:radius="7dp" />
<stroke
    android:width="3dp"
    android:color="@android:color/tertiary_text_light" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:padding="4dp"
android:text="some"
android:background="@drawable/textview_background"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="@android:color/black" />
使用name
list\u text\u view.xml制作布局

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/holo_blue_dark" />
<corners android:radius="7dp" />
<stroke
    android:width="3dp"
    android:color="@android:color/tertiary_text_light" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:padding="4dp"
android:text="some"
android:background="@drawable/textview_background"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="@android:color/black" />
您可以在活动中编写此代码

 ArrayList<String> list = new ArrayList<>();
    list.add("one");
    list.add("Two");
    list.add("Three");
    list.add("four");
    Spinner spinner = (Spinner) findViewById(R.id.SpinnerOneActivity_spinner);
    spinner.setAdapter(new TestAdapter(getApplicationContext(), R.layout.list_text_view, R.id.text1,list));
ArrayList list=new ArrayList();
列表。添加(“一”);
列表。添加(“两个”);
列表。添加(“三”);
列表。添加(“四”);
微调器微调器=(微调器)findViewById(R.id.SpinnerOneActivity\u微调器);
setAdapter(NewTestAdapter(getApplicationContext(),R.layout.list_text_view,R.id.text1,list));

您可以根据需要增加列表或动态生成内容,以便在列表连接到适配器(将提供给Spinner)后从列表中添加或删除,您可以调用
notifyDataSetChanged()
在适配器上更新列表

假设
textview\u background.xml
是您要将textview提供给
微调器的背景

在drawable中创建一个文件,并将其命名为
textview\u background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/holo_blue_dark" />
<corners android:radius="7dp" />
<stroke
    android:width="3dp"
    android:color="@android:color/tertiary_text_light" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:padding="4dp"
android:text="some"
android:background="@drawable/textview_background"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="@android:color/black" />
使用name
list\u text\u view.xml制作布局

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@android:color/holo_blue_dark" />
<corners android:radius="7dp" />
<stroke
    android:width="3dp"
    android:color="@android:color/tertiary_text_light" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:padding="4dp"
android:text="some"
android:background="@drawable/textview_background"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="@android:color/black" />
您可以在活动中编写此代码

 ArrayList<String> list = new ArrayList<>();
    list.add("one");
    list.add("Two");
    list.add("Three");
    list.add("four");
    Spinner spinner = (Spinner) findViewById(R.id.SpinnerOneActivity_spinner);
    spinner.setAdapter(new TestAdapter(getApplicationContext(), R.layout.list_text_view, R.id.text1,list));
ArrayList list=new ArrayList();
列表。添加(“一”);
列表。添加(“两个”);
列表。添加(“三”);
列表。添加(“四”);
微调器微调器=(微调器)findViewById(R.id.SpinnerOneActivity\u微调器);
setAdapter(NewTestAdapter(getApplicationContext(),R.layout.list_text_view,R.id.text1,list));
根据需要,您可以增加列表或动态生成内容,以便在列表连接到将提供给Spinner的适配器后从列表中添加或删除。您可以在适配器上调用
notifyDataSetChanged()
来更新列表

检查以下问题的答案:

有一个非常好的第三方开源项目叫做Android Wheel 这几乎完成了创建老虎机的所有工作 为你。如果你想定制它,它在Apache下 许可证,所以没有问题

检查以下问题的答案:

有一个非常好的第三方开源项目叫做Android Wheel 这几乎完成了创建老虎机的所有工作 为你。如果你想定制它,它在Apache下 许可证,所以没有问题


如果你定制你的旋转器,那么你可以用它做你想做的事情,这不是很容易吗?也许,但我不知道怎么做。我是android新手,尝试了一些东西。还没有成功。如果你能告诉我这将是多么感激。如果你定制你的纺纱机不是很容易吗,那么你可以用它做你想做的事,也许,但我不知道怎么做。我是android新手,尝试了一些东西。还没有成功。如果您能告诉我,我们将不胜感激。谢谢您的详细回复。在我最近的两次编辑中,我几乎做到了这一点——每一次都少了一个不同的小部分。在任何一种情况下,我都可以做一个简单的改变来实现我所需要的,而不是做你所建议的大的改变吗?当然,你可以做到。但那样的话,你只能改变文本背景和一些小的调整,这是我的编辑2我只需要设置主微调器(选中项目)的背景。我不知道怎么做。你能帮忙吗?抱歉@SteveW,直接将背景应用于微调器,你看不到预期的变化,这将改变微调器的背景,你必须实施更多的变化,让我看看我现在是否能简化你的工作。方程式微调器。收进地面资源(R。可绘制。圆);给我旋转器的背景,但形状不对。它应该显示为一个圆形,但其长度(椭圆形)与微调器的高度相同。我可以更改微调器的尺寸吗?谢谢您的详细回复。在我最近的两次编辑中,我几乎做到了这一点——每一次都少了一个不同的小部分。在任何一种情况下,我都可以做一个简单的改变来实现我所需要的,而不是做你所建议的大的改变吗?当然,你可以做到。但那样的话,你只能改变文本背景和一些小的调整,这是我的编辑2我只需要设置主微调器(选中项目)的背景。我不知道怎么做。你能帮忙吗?抱歉@SteveW,直接将背景应用于微调器,你看不到预期的变化,这将改变微调器的背景,你必须实施更多的变化,让我看看我现在是否能简化你的工作。方程式微调器。收进地面资源(R。可绘制。圆);给我旋转器的背景,但形状不对。它应该显示为一个圆形,但其长度(椭圆形)与微调器的高度相同。我可以更改spi的尺寸吗