无论如何要在 Actionbar Spinner 内添加两个文本视图或字符串?
类似于 Android GMAIL 应用程序的操作方式。
无论如何要在 Actionbar Spinner 内添加两个文本视图或字符串?
类似于 Android GMAIL 应用程序的操作方式。
您可以使用自定义 ArrayAdapter:
public class spinnerAdapter extends ArrayAdapter {
private LayoutInflater inflater;
public spinnerAdapter(Context context, int textViewResourceId) {
super(context, textViewResourceId);
inflater = LayoutInflater.from(context);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView == null){
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.spinner_text_layout, null);
holder.text1 = (TextView)convertView.findViewById(R.id.spinnerText1);
holder.text2 = (TextView)convertView.findViewById(R.id.spinnerText2);
convertView.setTag(R.layout.spinner_text_layout, holder);
} else{
holder = (ViewHolder)convertView.getTag(R.layout.spinner_text_layout);
}
holder.text1.setText("Position: " );
holder.text2.setText(position);
return convertView;
}
public View getDropDownView(int position, View convertView, ViewGroup parent) {
ViewHolder2 holder;
if(convertView == null){
holder = new ViewHolder2();
convertView = inflater.inflate(R.layout.spinner_text_layout, null);
holder.text1 = (TextView)convertView.findViewById(R.id.spinnerText1);
holder.text2 = (TextView)convertView.findViewById(R.id.spinnerText2);
convertView.setTag(R.layout.spinner_text_layout, holder);
} else{
holder = (ViewHolder2)convertView.getTag(R.layout.spinner_text_layout);
}
holder.text1.setText("Position: " );
holder.text2.setText(position);
return convertView;
}
static class ViewHolder{
TextView text1;
TextView text2;
}
static class ViewHolder2{
TextView text1;
TextView text2;
}
}
spinner_text_layout.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/spinnerText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="18dp"
android:layout_marginTop="16dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/spinnerText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_alignParentRight="true"
android:layout_marginRight="23dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
ActionBar.setListNavigationCallbacks(...)
这只是一个SpinnerAdapter
. 如果您熟悉适配器,则可以使自己的类扩展BaseAdapter
并具有自定义布局。如果您不熟悉适配器,我建议您观看The World of ListView。