2013-06-18 63 views

回答

3

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

+0

由user1888162提供的解決方案是正確的。您需要創建自己的自定義適配器併爲其提供包含兩個文本視圖的自定義行佈局。如果您需要更進一步,這可能會有所幫助http://dandar3.blogspot.in/2013/03/actionbarsherlock-custom-list-navigation.html – akoul889

相關問題