2012-10-21 77 views

回答

0

您可以參考下面。這代碼我的代碼會產生微調外觀上採用了下拉arrow.it由

實現android.R.layout.simple_spinner_item

通過指定ArrayAdapter。 改變它的風格,你可以使用不同的佈局,你可以找到here

import android.os.Bundle; 
import android.app.Activity; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.widget.AdapterView.OnItemSelectedListener; 

public class MainActivity extends Activity { 

    Spinner spin; 
    String spin_val; 
    String[] gender = { "Male", "Female" };//array of strings used to populate the spinner 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main);//setting layout 
     spin = (Spinner) findViewById(R.id.spinner_id);//fetching view's id 
     //Register a callback to be invoked when an item in this AdapterView has been selected 
     spin.setOnItemSelectedListener(new OnItemSelectedListener() { 

      @Override 
      public void onItemSelected(AdapterView<?> arg0, View arg1, 
        int position, long id) { 
       // TODO Auto-generated method stub 
       spin_val = gender[position];//saving the value selected 


      } 

      @Override 
      public void onNothingSelected(AdapterView<?> arg0) { 
       // TODO Auto-generated method stub 

      } 

     }); 
     //setting array adaptors to spinners 
     //ArrayAdapter is a BaseAdapter that is backed by an array of arbitrary objects 
     ArrayAdapter<String> spin_adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, gender); 

     // setting adapteers to spinners 
     spin.setAdapter(spin_adapter); 
    } 


} 

screen.xml

<Spinner 
     android:id="@+id/spinner_id" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" /> 

你在這裏有這樣的示範項目,供您參考here

希望這會有所幫助。