仅在自定义微调器

时间:2017-07-20 07:46:11

标签: java android

我使用带图标和文字的适配器创建了Spinner。一切都正确显示,但要求Activity微调器只显示文本。图标应仅出现在下拉列表中。  这有可能实现吗?

public class CustomAdapter extends BaseAdapter { 
 Context context; 
 int flags[]; 
 String[] countryNames; 
 LayoutInflater inflter; 

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

 [id3844467|@Override] 
 public int getCount() { 
 return flags.length; 
 } 

 [id3844467|@Override] 
 public Object getItem(int i) { 
 return null; 
 } 

 [id3844467|@Override] 
 public long getItemId(int i) { 
 return 0; 
 } 

 [id3844467|@Override] 
 public View getView(int i, View view, ViewGroup viewGroup) { 
 view = inflter.inflate(R.layout.custom_spinner_items, null); 
 ImageView icon = (ImageView) view.findViewById(R.id.imageView); 
 TextView names = (TextView) view.findViewById(R.id.textView); 
 icon.setImageResource(flags[i]); 
 names.setText(countryNames[i]); 
 return view; 
 } 
}     

XML

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

 <ImageView 
 android:id="@+id/imageView" 
 android:layout_width="40dp" 
 android:layout_height="40dp" 
 android:padding="5dp" 
 android:src="@drawable/ic_launcher" /> // for check that icon existing

 <TextView 
 android:id="@+id/textView" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:layout_gravity="center" 
 android:padding="0dp" 
 android:text="Demo" // for check that text existing
 android:textColor="#000" /> 

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

在spinner中设置这两个属性hopeFully它将起作用

android:dropDownHeight="0dp" 
android:dropDownWidth="0dp" 

答案 1 :(得分:0)

我不确定,但是改变你的getItemId:

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

并且不会在getItem上返回null