我認爲這將有助於你
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Simple Spinner Android Example
initSimpleSpinner();
//Android Custom Spinner Example Programmatically
initCustomSpinner();
}
private void initCustomSpinner() {
Spinner spinnerCustom = (Spinner) findViewById(R.id.spinnerCustom);
// Spinner Drop down elements
ArrayList<String> languages = new ArrayList<String>();
languages.add("Andorid");
languages.add("IOS");
languages.add("PHP");
languages.add("Java");
languages.add(".Net");
CustomSpinnerAdapter customSpinnerAdapter = new CustomSpinnerAdapter(SpinnerMainActivity.this, languages);
spinnerCustom.setAdapter(customSpinnerAdapter);
spinnerCustom.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "Android Custom Spinner Example Output..." + item, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void initSimpleSpinner() {
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Spinner Drop down elements
List<String> languages = new ArrayList<String>();
languages.add("Andorid");
languages.add("IOS");
languages.add("PHP");
languages.add("Java");
languages.add(".Net");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, languages);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String item = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), "Android Simple Spinner Example Output..." + item, Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public class CustomSpinnerAdapter extends BaseAdapter implements SpinnerAdapter {
private final Context activity;
private ArrayList<String> asr;
public CustomSpinnerAdapter(Context context, ArrayList<String> asr) {
this.asr = asr;
activity = context;
}
public int getCount() {
return asr.size();
}
public Object getItem(int i) {
return asr.get(i);
}
public long getItemId(int i) {
return (long) i;
}
@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
TextView txt = new TextView(SpinnerMainActivity.this);
txt.setPadding(16, 16, 16, 16);
txt.setTextSize(18);
txt.setGravity(Gravity.CENTER_VERTICAL);
txt.setText(asr.get(position));
txt.setTextColor(Color.parseColor("#000000"));
return txt;
}
public View getView(int i, View view, ViewGroup viewgroup) {
TextView txt = new TextView(SpinnerMainActivity.this);
txt.setGravity(Gravity.CENTER);
txt.setPadding(16, 16, 16, 16);
txt.setTextSize(16);
txt.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.ic_launcher, 0);
txt.setText(asr.get(i));
txt.setTextColor(Color.parseColor("#000000"));
return txt;
}
}
,並添加您的繪製文件
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
>
<solid android:color="@android:color/white"/>
<corners android:radius="4dp"></corners>
<stroke android:color="#cccccc" android:width="1dp"/>
</shape>
的Xml Filecode
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Simple Spinner"
android:layout_marginBottom="5dp"/>
<Spinner
android:spinnerMode="dropdown"
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Custom Spinner"
android:layout_marginBottom="5dp"/>
<Spinner
android:background="@drawable/spinner"
android:spinnerMode="dropdown"
android:id="@+id/spinnerCustom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
您可以使用自定義微調項目。 – Dexter
我已經使用自定義微調 –
我不認爲'dropDownSelector'是爲下拉的顏色... –