如何自定义微调器适配器

时间:2010-11-27 13:40:05

标签: android


我正在编写一个Android应用程序,我需要在左侧显示下拉列表,而在普通的Spinner控件中显示RadioButton。我想自定义微调控件。在Android中自定义Spinner时要遵循的步骤是什么。 在解决这个问题时,任何人都可以提供示例代码吗? 我将等待有价值的答复。

先谢谢,

1 个答案:

答案 0 :(得分:3)

微调器弹出项目可完全自定义。扩展ArrayAdapter< T>并覆盖getDropDownView方法。为每个项目调用此方法。这里的T类型是你给你的适配器的任何类型。

/** Expands the view and setup with the view for each item in spinner popup **/
@Override
public View getDropDownView(int position, View view, ViewGroup parent) {
   T choice = getItem(position);

   if (view == null) {
       LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

       // expand your list item here
       view = vi.inflate(R.layout.mylistitem, null);
    }
    if(choice != null) {
        // get whatever items are in your view
        TextView text = (TextView) view.findViewById(R.id.text);
        ImageView left = (ImageView) view.findViewById(R.id.leftImage);

        // do whatever you want with your item view 
    }
    return(view);
}

覆盖适配器的getView以设置主微调控件的视图。你可以打一个叫另一个,让选择和弹出看起来都一样。

相关问题