我使用下面的代码创建了一个微调器。现在我想使用一个名为 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);
我可以使用圆圈作为 TextView 中的背景,因此我尝试将 spinnerArray 创建为 TextView 数组,但这似乎与 simple_spinner_dropdown_item 不兼容。 有人知道我该怎么做吗?我只想让每个数字出现在旋转器的圆圈内。我还需要能够访问微调器中选择的号码。 谢谢
附注在 Android 中是否有任何方法可以像 iOS 中那样创建“老虎机”风格的旋转器?我更喜欢它们的外观。
编辑: 我现在发现可以通过创建自定义布局(称为 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" />
然后,我以编程方式为我的微调器设置适配器,如下所示:
ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.equations, R.layout.spinner_item);
adapter.setDropDownViewResource(R.layout.spinner_item);
equationSpinner.setAdapter(adapter);
但是,此方法要求我在 strings.xml 文件 (R.array.equations) 中设置预定义的字符串数组。我需要能够根据应用程序的状态指定要以编程方式使用的字符串数组。设置后我可以修改数组吗?
编辑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);
唯一的问题是能够将资源应用到微调器本身的背景(而不是下拉项)。主微调项目似乎没有与 setDropDownViewResource 等效的项。
最佳答案
假设textview_background.xml
是您想要为Spinner
的textView提供的背景
在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>
在您的 Activity 中创建一个 Spinner
<Spinner
android:id="@+id/SpinnerOneActivity_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
创建一个名为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" />
创建一个类,将列表中的数据填充到微调器中,在此代码中,您的适配器类正在获取字符串列表,但您可以为其提供自己的自定义对象列表来填充微调器,请注意适配器的构造函数参数。
第一个-上下文
第二个-布局,将赋予微调器中的每个项目
第三个-上面布局中的textView id
第四-您要填充的对象列表
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;
}
}
在您的 Activity 中,您可以编写此代码
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));
根据需要,您可以增加列表或动态生成内容,以便在列表附加到适配器后将其添加到列表中或从列表中删除,该适配器将提供给 Spinner,您可以在适配器上调用 notifyDataSetChanged()
来更新列表
关于java - 使用可绘制资源为微调器中的项目设置背景 - java android studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33694802/