Android: Custom Spinner Layout

匿名 (未验证) 提交于 2019-12-03 01:57:01

问题:

I am trying to make a fully custom spinner. I am running into difficulties with making the layout that pops up when you press on it. Here is my code for my adapter:

    ArrayAdapter adapter = ArrayAdapter.createFromResource(             this, R.array.my_array, R.layout.spinnertext);     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);     spinner.setAdapter(adapter); 

From what I have read in the documentation, the layout used apears to be set by the line:

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

Although every time I change it to a new layout that I make, it makes the app fail when I try and use the spinner. I have tried to look for what "android.R.simple_spinner_dropdown_item" looks like so as to figure out if I am maybe missing anything.

All of my layouts I have tried have been linear or relative layouts, with only a textView.

How can I make a custom layout pop up when the spinner is selected?

回答1:

row.xml to set up the layout on each row (in this case: one image and text each row):

Java:

public class AndroidCustomSpinner extends Activity {   String[] DayOfWeek = {"Sunday", "Monday", "Tuesday",    "Wednesday", "Thursday", "Friday", "Saturday"};     /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);         Spinner mySpinner = (Spinner)findViewById(R.id.spinner);        ArrayAdapter adapter = new ArrayAdapter(this,          R.layout.row, R.id.weekofday, DayOfWeek);        mySpinner.setAdapter(adapter);    } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!