【问题标题】:how to add a small triangle in spinner? [duplicate]如何在微调器中添加一个小三角形? [复制]
【发布时间】:2015-11-20 19:09:25
【问题描述】:

如您在代码中所见,我实现了自己的自定义适配器微调器。 除了图像从左侧附加到每个值而不是仅附加到所选值之外,一切正常。 在下图中,这就是我得到的:

这是我的代码:

public class MyAdapter extends ArrayAdapter<String> {

    private Context context;
    private String[] values;

    public MyAdapter(Context ctx, int txtViewResourceId, String[] objects) {
        super(ctx, txtViewResourceId, objects);
        context = ctx;
        values = objects;
    }

    @Override
    public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
        return getCustomView(position, cnvtView, prnt);
    }
    @Override
    public View getView(int pos, View cnvtView, ViewGroup prnt) {
        return getCustomView(pos, cnvtView, prnt);
    }
    public View getCustomView(int position, View convertView,ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View mySpinner = inflater.inflate(R.layout.custom_spinner, parent,
                false);
        TextView subSpinner = (TextView) mySpinner.findViewById(R.id.sub_text_seen);
        subSpinner.setText(values[position]);
        ImageView right_icon = (ImageView) mySpinner.findViewById(R.id.left_pic);


        return mySpinner;
    }
}

这是适配器的初始化:

spinnerduration.setAdapter(new MyAdapter(this, R.layout.custom_spinner, weeks_array));
    spinnerduration.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            /*TextView selectedText = (TextView) parent.getChildAt(0);
            if (selectedText != null) {
                selectedText.setTextColor(Color.WHITE);
            }*/
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            /*TextView selectedText = (TextView) parent.getChildAt(0);
            if (selectedText != null) {
                selectedText.setTextColor(Color.WHITE);
            }*/
        }
    });

【问题讨论】:

  • 设置Spinner背景图片
  • 什么意思?而是使用刚刚为微调器设置背景的图像?
  • 我应该删除 ImageView 对象吗?并在 clickListener 中设置背景?
  • 您必须为getDropDownViewgetView 使用两个不同的视图

标签: android


【解决方案1】:

你需要迭代可见的项目,如果它们没有被选中,你应该删除图像:

selectedItem.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);

如果它被选中,你需要设置你的三角形可绘制:

selectedItem.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon, 0, 0, 0);

【讨论】:

  • 我应该在哪里使用它?在 onSelectedItemListener 中?
  • 如何使用适配器?可能,您使用适配器类的实例设置微调器适配器。在那里你也可以设置它的背景。
  • 是的,我从 MyAdapter 创建了一个新实例。设置适配器后是否应该设置背景?
  • 好的,我明白你的意思并编辑了我的答案。
  • 你能看看我的代码吗?我编辑了他,但我仍然不明白如何使用“selectedItem”对象。
猜你喜欢
  • 2013-12-23
  • 2014-11-12
  • 2015-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-07
  • 2014-11-12
相关资源
最近更新 更多