在Android上使用微调器

时间:2013-07-26 01:54:17

标签: android spinner

我希望实现微调器有2个以下的请求:
1.在显示选择项目列表时,文本左对齐如图4所示 2.选择一个项目后,我想让所选的一个显示右对齐,如图1所示 我的微调器布局微调器布局如下:
                        

                        <Spinner
                            android:id="@+id/spinner1"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:layout_alignParentRight="true"
                            android:layout_centerVertical="true"
                            android:textColor="#000000"
                            android:gravity="right"
                            android:textAppearance="?android:attr/textAppearanceMedium" />

                    </RelativeLayout>

和Adapter的getView如下:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewTag viewTag;
    if(convertView == null) {
        convertView = myInflater.inflate(R.layout.row_spinner, null);
        viewTag = new ViewTag((TextView)convertView.findViewById(R.id.textView1));
        convertView.setTag(viewTag);
    }
    else {
        viewTag = (ViewTag)convertView.getTag();
    }

    viewTag.line1.setText(gd.Lang.get(position));
    viewTag.line1.setTextColor(Color.BLACK);
    viewTag.line1.setSelected(true);
    viewTag.line1.setPadding(0, 0, 40, 0);
    viewTag.line1.setGravity(Gravity.RIGHT);

    return convertView;
}

但它会显示为Picture.1和Picture.2 如果我将行viewTag.line1.setGravity(Gravity.RIGHT);更改为viewTag.line1.setGravity(Gravity.LEFT); 它将显示为Picture.3和Picture.4 如何修改同时到达Picture.1和Picture.4?

Picture.1
enter image description here
Picture.2
enter image description here
Picture.3
enter image description here
Picture.4
enter image description here

3 个答案:

答案 0 :(得分:2)

您需要在适配器中设置getView()和getDropDownView()

getView()将为你的图片1设置布局,getDropDownView()设置 - 如名称所示 - 为你的图片4下拉视图。

检查this well written answer

答案 1 :(得分:1)

您可以在适配器类中处理所有这些逻辑,在getView函数中,您返回的视图将具有所需的文本对齐方式。当项目单击存储适配器类中的位置时,使用abase适配器和函数。并且在您的getview中,根据位置,您可以拥有任何对齐逻辑。

答案 2 :(得分:1)

如果我们查看以下代码,我们在getView()getDropDownView()中有名称和值数组。

private void initView() {
    SpinnerDropDownAdapter sddadapter = new SpinnerDropDownAdapter(this);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, sddadapter.name);

    Spinner getViewSP = (Spinner) findViewById(R.id.getview_sp);
    getViewSP.setAdapter(adapter);

    Spinner getViewWDropDownSP = (Spinner) findViewById(R.id.getview_w_drop_down_sp);
    getViewWDropDownSP.setAdapter(sddadapter);
}

static class SpinnerDropDownAdapter extends BaseAdapter implements
        SpinnerAdapter {
    Context context;

    SpinnerDropDownAdapter(Context ctx) {
        context = ctx;
    }

    String[] name = { " One", " Two", " Three", " Four", " Five", " Six",
            " Seven", " Eight" };
    String[] value = { " 1", " 2", " 3", " 4", " 5", " 6", " 7", " 8" };

    @Override
    public int getCount() {
        return name.length;
    }

    @Override
    public String getItem(int pos) {
        // TODO Auto-generated method stub
        return name[pos];
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView text = new TextView(context);
        text.setTextColor(Color.BLACK);
        text.setText(name[position]);
        return text;
    }

    @Override
    public View getDropDownView(int position, View convertView,
            ViewGroup parent) {
        TextView text = new TextView(context);
        text.setTextColor(Color.BLACK);
        text.setText(value[position]);
        return text;
    }
}