1

我为我的微调器设置了背景图像,但是当我从微调器的下拉列表中选择一个项目时,我的微调器背景图像被所选项目替换,我希望我的背景图像保留在那里并且我不希望显示所选项目而不是微调器的背景图像。我怎么做?

我的自定义适配器的代码:

public class MyAdapter extends ArrayAdapter<String> {

    public MyAdapter(Context ctx, int txtViewResourceId, String[] objects) {
        super(ctx, txtViewResourceId, 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 = getLayoutInflater();
        View mySpinner = inflater.inflate(R.layout.custom_spinner, parent,
                false);
        TextView main_text = (TextView) mySpinner
                .findViewById(R.id.text_main_seen);
        main_text.setText(spinnerValues[position]);





        return mySpinner;
    }
}

我的自定义微调器 xml 的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
     >

    <TextView
        android:id="@+id/text_main_seen"
        android:layout_width="fill_parent"
        android:layout_height="30sp"
        android:background="#008000"
        android:gravity="center"

        android:textColor="#ffffff"
        android:textSize="30px" />

</RelativeLayout>

我的实际 spinner.xml 的代码

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Spinner
        android:id="@+id/planets_spinner"
        android:layout_width="100dp"
        android:layout_height="50sp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:background="#008000"
        android:layout_weight="0.08"
        android:drawSelectorOnTop="true" />

</RelativeLayout

我调用微调器的 mainActivity 的代码是

Spinner mySpinner = (Spinner) findViewById(R.id.planets_spinner);
    mySpinner.setAdapter(new MyAdapter(this, R.layout.custom_spinner,
            spinnerValues));
4

1 回答 1

0

我根本不会为此使用微调器。只需使用一个看起来像 Spinner 的 Button 或任何您希望它看起来像的其他东西。您可以使用此属性使常规 Button 看起来像 Spinner:

android:background="@android:drawable/btn_dropdown"

或者,制作您自己的看起来像 Spinner 的背景并使用它。

然后,让您的 Button 的 onClick 事件打开一个“单项选择”对话框。

于 2013-10-11T14:24:48.130 回答