2017-02-13 6 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, что это за макет? I'm puzzed.er ..... .. –

+0

Я отредактировал ответ, это проще, чем раньше. Пожалуйста, дайте мне отзыв. – tahsinRupam

+0

yep, я сделал it.it полезным.cuz механизм этого веб-сайта, я не могу голосовать за вас, пока я не получу 15 репутации. –

Смежные вопросы