开发者

Custom style setDropDownViewResource Android Spinner

Below is my current code that adds my array_spinner to my adapter and then adds the adapter to my spinner. QUESTIONS:

1- I am not sure how I would use the following code to style my drop down: areaspinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); and how do I apply a custom view/text view? I need to style the text item in the spinner drop down.

2- Do I need to create a a separate layout.xml file for this view?

Any info would be great. Thanks!

array_spinner=new String[4];
array_spinner[0]="Select";
array_spinner[1]="George Washington";
array_spinner[2]="Benjamin Franklin";
array_spinner[3]="Thomas Jefferson";

 //CREATE SPINNER
areaspinner = (Spinner) findViewById(R.id.areaspinner);

ArrayAdapter<String> adapter = 
    new ArrayAdapter<String> (thi开发者_JS百科s, android.R.layout.simple_spinner_item,array_spinner);
areaspinner.setAdapter(adapter);


  1. If your intention is to achieve different styling of drop down list items with respect to spinner view selected item styling, you have to indeed provide your custom layout id to setDropDownViewResource(). In your layout you just have to declare TextView as root element because it is required by ArrayAdapter

  2. Yes, you need to create separate your_layout_name.xml file in res/layout directory for each layout you declare.


This is how you use the setDropDownViewResource() in your code

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

Sure it helps.


You don't have to use setDropDownViewResource(), you can simply provide your own theme, and override the style. See my answer here: Android Spinner - How to make dropdown view transparent?


Define a customer spinner_layout.xml with TextView as root view. When setting the adapter layout, use the following code:

ArrayAdapter<String> adapter1 = new ArrayAdapter<>(getActivity(), R.layout.layout_spinner, days);

OR

adapter1.setDropDownViewResource(R.layout.layout_spinner);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜