android 更改spinner默认样式

效果图:

android 更改spinner默认样式_第1张图片


一张图片(Spinner背景图片,带白色箭头的透明图片),一个Activity布局(背景为蓝色)和两个样式布局

1.Activity布局

 
  


    
    
    



2.adapter_mytopactionbar_spinner  可以更改默认样式的布局

 
  


    



3.
adapter_mytopactionbar_spinner_item  设置spinner展开的Item布局
 
  





 
  
Activity 中
 
  
 
  
public class SpinnerActivity extends Activity {
    private Spinner spinner;
    private static String[] mArrayString = null;
    private ArrayAdapter mArrayAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test_spinner_layout);
        spinner=(Spinner)findViewById(R.id.test_spinner);
//        初始化spinner中显示的数据
        mArrayString = new String[]{"android讲义","java面向对象","王者归来"};
//        adapter_mytopactionbar_spinner改变了spinner的默认样式
        mArrayAdapter=new ArrayAdapter(this,R.layout.adapter_mytopactionbar_spinner,mArrayString){
            @Override
            public View getDropDownView(int position, View convertView, ViewGroup parent) {
                if (convertView == null){
//                    设置spinner展开的Item布局
                    convertView = getLayoutInflater().inflate(R.layout.adapter_mytopactionbar_spinner_item, parent, false);
               }
                TextView spinnerText=(TextView)convertView.findViewById(R.id.spinner_textView);
                spinnerText.setText(getItem(position));
              return convertView;
            }
        };
        spinner.setAdapter(mArrayAdapter);
//        spinner设置监听
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                Toast.makeText(SpinnerActivity.this,"你选的是的是第"+position,Toast.LENGTH_SHORT).show();
            }
            @Override
            public void onNothingSelected(AdapterView parent) {

            }
        });


     }
}



你可能感兴趣的:(Android)