6

我的观点是这样的,我正在为微调器使用自定义适配器

我已经从这个链接 http://rimpv.blogspot.in/2013/05/bind-spinner-dropdown-in-android.html设计了我的微调器

@Override
public View getView(int position, View convertView, ViewGroup parent) {
            TextView v = new TextView(getApplicationContext());
            v.setTextColor(Color.BLACK);
            v.setText(data.get(position).name);
            return v;
        }

在此处输入图像描述

但我想像这样展示在此处输入图像描述

如何在自定义微调器中执行此操作?提前感谢您的帮助

4

5 回答 5

1

编辑 3

这个例子只是从头开始,现在没有IDE来测试它。

如果您不需要自定义适配器,则可以使用标准 ArrayAdapter。然后为您的适配器设置 dropDownViewResource。

public class Main extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.setContentView(R.layout.main);
    Spinner s = (Spinner) this.findViewById(R.id.spinner);

    ArrayList<String> list = new ArrayList<String>();
    list.add("Germany");
    list.add("USA");
    list.add("Nairobi");
    list.add("Japan");

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, list);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    s.setAdapter(adapter);

    s.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {

            Toast.makeText(getApplicationContext(),
                    "CLICKED:"+parent.getItemAtPosition(pos).toString(),
                    Toast.LENGTH_LONG).show();
        }

        public void onNothingSelected(AdapterView parent) {
          // Do nothing.
        }
    });
}

但是如果您需要一个 customAdapter,那么除了构建您自己的微调器项目布局之外别无他法:

构建你的 main.xml

   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:orientation="vertical"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
    >
   <Spinner 
      android:drawSelectorOnTop="true"
      android:id="@+id/example_spinner" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content"  > 
   </Spinner>
  </LinearLayout>

构建您的 Spinner 项目 xml:spinner_item.xml:

    <LinearLayout 
      xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/spinner_item_linear_layout" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content"
       android:orientation=”vertical”&gt;


    <TextView        
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content "
      android:id="@+id/spinner_textView" >        
    </TextView>

    <RadioButton
      android:layout_width=”wrap_content”
      android:layout_height=”wrap_content”
      android:id=”@+id/spinner_radio_button” >
   </RadioButton>

 </LinearLayout>

使用一些输入定义一个字符串数组,创建您的自定义适配器并将适配器设置为您的微调器。

    public class CustomSpinnerExample extends Activity {


  String []countries ={"Germany","USA","Nairobi","Japan"};

    @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Spinner mySpinner = (Spinner)findViewById(R.id.example_spinner);
    mySpinner.setAdapter(new CustomAdapter(this, R.layout.spinner_item, countries));
   }


    public class CustomAdapter extends ArrayAdapter<String>
   {

        public CustomAdapter(Context context, int resourceId,
                    String[] objects) {
              super(context, resourceId, objects);
              // TODO Auto-generated constructor stub
        }
        @Override
     public View getDropDownView(int position, View convertView,ViewGroup parent) {
        return getCustomView(position, convertView, parent);
     }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        return getCustomView(position, convertView, parent);
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater=getLayoutInflater();
        View row=inflater.inflate(R.layout.spinner_item, parent, false);
        TextView label=(TextView)row.findViewById(R.id.spinner_textView);
         label.setText(countries[position]);


        RadioButton radioButton    =(RadioButton)row.findViewById(R.id.spinner_radio_button);
       radioButton.setOnClickListener(new OnClickListener(){

   public void onClick(View v){

Toast.makeText(CustomAdapter.this,”CLICKED:”+label.getText(),Toast.LENGTH_LONG).show();

  }
    });

        return row;
        }

   }
}
于 2013-05-16T12:23:34.933 回答
1

您可以使用源代码创建一个新的“doubleline_spinner.xml”

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="false"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:ellipsize="none" 
android:textSize="15dp"

/>

并将其添加到您的微调器中

        ArrayAdapter<CharSequence> PolarityAAdapter = ArrayAdapter.createFromResource(this,
        R.array.polarity_arrays,android.R.layout.simple_spinner_item);
        PolarityAAdapter.setDropDownViewResource(R.layout.doubleline_spinner);
        PolarityAspinner.setAdapter(PolarityAAdapter);
于 2013-09-30T05:35:15.640 回答
0

You can take a look to this a simple tutorial like this. But I'm really sure you don't want to do this.

So take a look on How to make a custom adapter with layout, tutorial is for listView but it's the same operation for spinner.

You can create your own layout that contain a TextView and a CheckBox like that :

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
android:id=”@+id/linearLayout1″
android:layout_width=”fill_parent”
android:layout_height=”fill_parent” >

<CheckBox
android:id=”@+DropDownList/checkbox”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />

<TextView
android:id=”@+DropDownList/SelectOption”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />

</LinearLayout>

and in your function :

@Override
    public int getItemViewType(int position) {
        return R.layout.YourLayout;
    }
于 2013-05-16T12:27:31.070 回答
0

看看这个教程

您可以更改微调器的背景图像:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Spinner
    android:drawSelectorOnTop="true"
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:prompt="@string/select">
    </Spinner>
</LinearLayout>

通过添加 :

android:src="@drawable/your_image"

到你的微调器。所以它看起来像:

<Spinner
android:drawSelectorOnTop="true"
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/your_image"
android:prompt="@string/select">
</Spinner>
于 2013-05-16T12:30:22.660 回答
0

这是getDropDownView

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null)
        convertView = inflater.inflate(R.layout.simple_spinner_dropdown_item, null, true);

    CheckedTextView text = (CheckedTextView) convertView.findViewById(R.id.text1);

    text.setText("TEXT");

    return convertView;
}

并在创建适配器后添加

myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
于 2015-04-11T14:32:28.473 回答