简体   繁体   English

使用可绘制资源为微调器中的项目设置背景-Java Android Studio

[英]Set background for items in a spinner using a drawable resource - java android studio

I've created a spinner using the code below. 我使用以下代码创建了一个微调器。 Now I want to use a drawable resource called circle.xml, to set the background for each item in the spinner so that each number appears inside a circle. 现在,我想使用一个名为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);

I can use the circle as a background in a TextView so I tried creating the spinnerArray as an array of TextViews but that appeared to be incompatible with the simple_spinner_dropdown_item. 我可以将圆圈用作TextView中的背景,所以我尝试将spinnerArray创建为TextViews的数组,但似乎与simple_spinner_dropdown_item不兼容。 Anybody have an idea how I can do this? 有人知道我该怎么做吗? I just want each number to appear inside a circle in the spinner. 我只希望每个数字出现在微调器的圆圈内。 I will also need to be able to access the number selected in the spinner. 我还需要能够访问微调器中选择的号码。 Thanks 谢谢

PS Is there any way in Android to create a 'slot machine' style spinner like you have in iOS? PS与Android一样,Android中有什么方法可以创建“老虎机”风格的微调器? I prefer the look of those. 我更喜欢那些外观。

EDIT: I've now found that I can use my circle resource by creating a custom layout (called spinner_item.xml) like this: 编辑:我现在发现我可以通过创建这样的自定义布局(称为spinner_item.xml)来使用我的圈子资源:

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

I then set the adapter for my spinner programmatically like this: 然后,以编程方式为我的微调器设置适配器,如下所示:

ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.equations, R.layout.spinner_item);
        adapter.setDropDownViewResource(R.layout.spinner_item);
        equationSpinner.setAdapter(adapter);

However, this method requires that I have a predefined string array set up in my strings.xml file (R.array.equations). 但是,此方法要求我在strings.xml文件(R.array.equations)中设置了预定义的字符串数组。 I need to be able to specify the array of strings to be used programmatically depending on the state of the app. 我需要能够根据应用程序的状态指定要以编程方式使用的字符串数组。 Can I modify the array once its set up? 阵列设置好后是否可以对其进行修改?

EDIT 2: Additionally I've found I can set the background of the dropdown items and use my own programmatical array like this: 编辑2:此外,我发现我可以设置下拉菜单项的背景,并使用自己的程序化数组,如下所示:

ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, spinnerArray);
        spinnerArrayAdapter.setDropDownViewResource(R.layout.spinner_item);
        equationSpinner.setAdapter(spinnerArrayAdapter);

The only problem then is being able to apply the resource to the background of the spinner itself (not the drop down items). 唯一的问题就是能够将资源应用于微调框本身的背景(而不是下拉菜单项)。 There does not appear to be an equivalent to setDropDownViewResource for the main spinner item. 主微调器项目似乎没有与setDropDownViewResource等效的对象。

lets say textview_background.xml is the background you want to give to the textView to Spinner 可以说textview_background.xml是您要给textView给Spinner的背景

make a file in drawable and name it this textview_background.xml 在drawable中创建一个文件并将其命名为textview_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>

make a Spinner in your activity 做一个Spinner你的活动

 <Spinner
    android:id="@+id/SpinnerOneActivity_spinner"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

make a layout with name list_text_view.xml 使用名称list_text_view.xml进行布局

<?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" />

make a class which will fill data from a list to your spinner, in this code your Adapter class is taking list of String but you can have your own custom object list given to it to populate the spinner, watch out how adapter's constructor parameters. 创建一个将数据从列表填充到微调器的类,在此代码中,您的Adapter类正在获取String的列表,但是您可以给自己提供自定义对象列表以填充微调器,请注意适配器的构造函数参数。

first- Context 第一Context

Second- layout which will be given to each item in spinner 第二个layout which will be given to each item in spinner

Third- a textView id from the above layout 第三- a textView id from the above layout

Forth- your list of objects which you want to populate 第四- your list of objects which you want to populate

TestAdapter.java

public class TestAdapter extends ArrayAdapter {

private ArrayList<String> strings;
private Context context;
private LayoutInflater layoutInflater;

public TestAdapter(Context context, int resource,int id, ArrayList<String> strings) {
    super(context, resource, id, strings);
    this.context = context;
    this.strings = strings;
    this.layoutInflater = LayoutInflater.from(this.context);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = layoutInflater.inflate(R.layout.list_text_view, parent, false);
    TextView textView = (TextView) view.findViewById(R.id.text1);
    textView.setText("" + strings.get(position));
    return view;
}
}

in your activity you can write this code 在您的活动中,您可以编写此代码

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

As you wanted you can increase the list or generate things dynamically to add or remove from the list once your list is attached to adapter which will be provided to Spinner you can call notifyDataSetChanged() on adapter to updated the list 如您所愿,您可以增加列表或动态生成事物,以将列表附加到将提供给Spinner的适配器上后,将其添加到列表中或从列表中删除,您可以在适配器上调用notifyDataSetChanged()来更新列表

Check this answer about Android Wheel : https://stackoverflow.com/a/9503372/3677394 检查有关Android Wheel的以下答案: https : //stackoverflow.com/a/9503372/3677394

There's a very good 3rd party open source project called Android Wheel that pretty much does everything involved in creating a slot machine for you. 有一个非常好的第三方开源项目,称为Android Wheel,它几乎可以为您创建投币机。 And if you want to customise it, it's under an Apache licence, so no issues there. 而且,如果您想对其进行自定义,它是在Apache许可下的,因此那里没有问题。

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

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