Android:向Actionbar Spinner添加两个TextView

时间:2013-06-18 18:58:59

标签: android textview android-actionbar spinner

无论如何在Actionbar Spinner中添加两个文本视图或字符串?

类似于Android GMAIL应用程序的功能。

2 个答案:

答案 0 :(得分:4)

您可以使用自定义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" />

答案 1 :(得分:0)

ActionBar.setListNavigationCallbacks(...)

这只是一个SpinnerAdapter。如果您熟悉适配器,则可以使自己的类扩展BaseAdapter并具有自定义布局。如果您不熟悉适配器,我建议您观看The World of ListView