8

我有基本的 android 微调器,单击它后,我希望列表中的项目突出显示其中一个项目,即最初选择的项目。

就像在这里完成的那样: http ://www.warriorpoint.com/blog/wp-content/uploads/2009/05/05spinner-thumb.png

但是使用我自己的项目布局而不是单选框,而是使用我自己的背景。

我怎样才能做到这一点?选择器中有什么用处,还是我必须以编程方式进行?

任何帮助表示赞赏。

4

4 回答 4

19

以下是我测试和验证的解决方案。您可以使用 setSelection(N) 突出显示您想要的项目。

class HighLightArrayAdapter extends ArrayAdapter<CharSequence> {

        private int mSelectedIndex = -1;


        public void setSelection(int position) {
            mSelectedIndex =  position;
            notifyDataSetChanged();
        }

        public HighLightArrayAdapter(Context context, int resource, CharSequence[] objects) {
            super(context, resource, objects);
        }


        @Override
        public View getDropDownView(int position, View convertView, ViewGroup parent) {
            View itemView =  super.getDropDownView(position, convertView, parent);

            if (position == mSelectedIndex) {
                itemView.setBackgroundColor(Color.rgb(56,184,226));
            } else {
                itemView.setBackgroundColor(Color.TRANSPARENT);
            }

            return itemView;
        }
    }
于 2015-10-29T03:44:00.357 回答
5
public class MainActivity extends Activity {

Spinner mySpinner;
String[] myArray={"1","2","3","4","5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mySpinner = (Spinner)findViewById(R.id.spinner_test);

    mySpinner.setAdapter(new MyAdapter(this,android.R.layout.simple_spinner_item, myArray));

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

public class MyAdapter extends ArrayAdapter<String>{

    public MyAdapter(Context context, int textViewResourceId,
            String[] objects) {
        super(context, textViewResourceId, objects);
        // TODO Auto-generated constructor stub
    }

    @Override
    public View getDropDownView(int position, View cnvtView, ViewGroup prnt){
        LayoutInflater inflater = getLayoutInflater();
        View spinnerItem = inflater.inflate(android.R.layout.simple_spinner_item, null);

        TextView mytext= (TextView)spinnerItem.findViewById(android.R.id.text1);
        mytext.setText(myArray[position]);

        //int selected = Spinner.
        int selected = mySpinner.getSelectedItemPosition();
        if(position == selected){
            spinnerItem.setBackgroundColor(Color.BLUE);
        }
        return spinnerItem;

    }

}

}

这应该会有所帮助。

于 2013-12-27T21:43:44.647 回答
2

Kotlin 版本更直接:

    val spinner = Spinner(context)
    val adapter = object: ArrayAdapter<Any>(context, android.R.layout.simple_spinner_item, choices.toTypedArray()) {
        override fun getDropDownView(position: Int, convertView: View?, parent: ViewGroup): View {
            return super.getDropDownView(position, convertView, parent).also { view ->
                if(position == spinner.selectedItemPosition)
                    view.setBackgroundColor(Color.rgb(204, 255, 255))
            }
        }
    }
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item
    spinner.adapter = adapter

您显然可以设置视图的其他属性 - 返回的视图android.R.layout.simple_spinner_dropdown_item是的子类,TextView因此您可以设置文本对齐和类似属性,例如右对齐文本:

                (view as? TextView)?.textAlignment = TEXT_ALIGNMENT_VIEW_END
于 2019-07-09T22:24:26.167 回答
-2

要回答我的问题,你需要有这样的东西:

public class mySpinnerAdapter extends SimpleCursorAdapter implements SpinnerAdapter {

public OrderSpinnerAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
    super(context, layout, c, from, to);
    // TODO whatever you need
}

public View getDropDownView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(android.R.layout.simple_dropdown_item_1line, parent, false);
    }

    // TODO set the background color of the convertView
    // depending on your wishes

    return convertView;
}
}

这样我们就可以像这样控制下拉列表的创建。如果您需要不同的选择器,您可以在 XML 文件中轻松完成。

然后,当您只是简单地创建适配器并使用方法 setAdapter 将它们绑定到微调器时。

于 2012-09-18T12:57:32.310 回答