简体   繁体   English

android spinner和数组适配器以编程方式

[英]android spinner and array adapter programmatically

I am trying to programmatically create a Spinner using an ArrayAdapter. 我试图使用ArrayAdapter以编程方式创建一个Spinner。 This is being done in a closed source jar as part of an Android library project and I do not have any access to resources. 这是在一个封闭的源jar中完成的,作为Android库项目的一部分,我没有任何资源访问权限。

I want to know how to customize the layout and the text view that displays the spinner. 我想知道如何自定义布局和显示微调器的文本视图。

--EDIT-- - 编辑 -
Presently I am using the following code 目前我正在使用以下代码

ArrayAdapter<T> spinnerData = new ArrayAdapter<T>(this, Android.R.layout.simple_spinner_item, T[]);

spinner.setAdapter(spinnerData);

What is happening is that I cannot control the size of the fonts, colours or anything else. 发生的事情是我无法控制字体,颜色或其他任何东西的大小。 When I click on the spinner to select something all the options are in really small text. 当我点击微调器选择某些东西时,所有选项都是非常小的文本。 I want to be able to change the way it renders on screen. 我希望能够改变它在屏幕上呈现的方式。

Just create a custom ArrayAdapter and define how the spinner textview should look in that getDropDownView method of the adapter 只需创建一个自定义ArrayAdapter并定义微调器textview在适配器的getDropDownView方法中的外观

public class CustomizedSpinnerAdapter extends ArrayAdapter<String> {

 private Activity context;
 String[] data = null;

 public CustomizedSpinnerAdapter(Activity context, int resource, String[] data2)
 {
     super(context, resource, data2);
     this.context = context;
     this.data = data2;
 }
  ...

 @Override
 public View getDropDownView(int position, View convertView, ViewGroup parent)
 {   
     View row = convertView;
     if(row == null)
     {
         //inflate your customlayout for the textview
         LayoutInflater inflater = context.getLayoutInflater();
         row = inflater.inflate(R.layout.spinner_layout, parent, false);
     }
     //put the data in it
     String item = data[position];
     if(item != null)
     {   
        TextView text1 = (TextView) row.findViewById(R.id.rowText);
        text1.setTextColor(Color.WHITE);
        text1.setText(item);
     }

     return row;
 }
...
}

and set this adapter for the spinner 并为微调器设置此适配器

Spinner mySpinner = (Spinner) findViewById(R.id.mySpinner);
final String[] data = getResources().getStringArray(
            R.array.data);
final ArrayAdapter<String> adapter1 = new CustomizedSpinnerAdapter(
            AddSlaveActivity.this, android.R.layout.simple_spinner_item,
            data);
mySpinner.setAdapter(adapter1); 

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

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