2017-02-13 45 views

回答

0

文字和ImageView的創建自定義適配器:

public class CustomSpinnerAdapter extends BaseAdapter { 
Context context; 
int image[]; 
String[] text; 
LayoutInflater inflter; 

public CustomAdapter(Context applicationContext, int[] image, String[] text) { 
    this.context = applicationContext; 
    this.image = image; 
    this.text = text; 
    inflter = (LayoutInflater.from(applicationContext)); 
} 

@Override 
public int getCount() { 
    return flags.length; 
} 

@Override 
public Object getItem(int i) { 
    return null; 
} 

@Override 
public long getItemId(int i) { 
    return 0; 
} 

@Override 
public View getView(int i, View view, ViewGroup viewGroup) { 
    view = inflter.inflate(R.layout.spinner_item_layout, null); 
    ImageView icon = (ImageView) view.findViewById(R.id.spImageView); 
    TextView names = (TextView) view.findViewById(R.id.spTextView); 
    icon.setImageResource(image[i]); 
    names.setText(text[i]); 
    return view; 
} 

}

然後設置這個適配器微調:

CustomSpinnerAdapter adapter = new CustomSpinnerAdapter(this, YourImageArray, YourTextArray); 
     spinner.setAdapter(adapter); 

下面是每個紡紗項目(spinner_item_layout佈局。 xml):

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal"> 

     <TextView 
      android:id="@+id/spTextView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" /> 

     <ImageView 
      android:id="@+id/spImageView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:contentDescription="@string/app_name"/>" 

</LinearLayout> 

希望這會有所幫助。 :)

+0

我是一個綠色的手,所以,佈局之間的區別是什麼:spinner_value_layout和spinner_item_layout.in換句話說,如何編寫spinner_value_layout,這個佈局是什麼?我很困惑。 .. –

+0

我編輯了答案,這比以前更簡單。請給我一個反饋。 – tahsinRupam

+0

是的,我做了它。它的有用.cuz這個網站的機制,我不能爲你投票,直到我再次獲得15聲望。感謝。 –

相關問題