android 华为Mate 10 spinner 点击区域bug
1. testAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, dataList);
用自带的 ArrayAdapter 会导致点击区域变小
2. testAdapter =new MyAdapter(this, dataList);
自定义 Adapter 之后点击区域变成正常
源代码
public class MainActivityextends AppCompatActivity {
private SpinnermySpinner;
private ListdataList =new ArrayList<>();
private MyAdaptertestAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i =0; i <10; i++) {
if (i %2 ==0) {
dataList.add("西瓜");
}else {
dataList.add("苹果");
}
}
mySpinner = findViewById(R.id.my_spinner);
// testAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, dataList);
// testAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
testAdapter =new MyAdapter(this, dataList);
mySpinner.setAdapter(testAdapter);
}
private class MyAdapterextends BaseAdapter {
private ListdataList;
private ContextmContext;
private MyAdapter(Context context, List stringList) {
this.dataList = stringList;
this.mContext = context;
}
@Override
public int getCount() {
return dataList ==null ?0 :dataList.size();
}
@Override
public StringgetItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public ViewgetView(int position, View convertView, ViewGroup parent) {
View rootView;
//如果没有可以重用的控件
if (convertView ==null) {
LayoutInflater inflater = LayoutInflater.from(mContext);
rootView = inflater.inflate(R.layout.spinner_add_ticket_item, parent, false); //加载布局,创建View
rootView.setTag(position);
}else {
//控件己经被创建过,直接重用
rootView = convertView;
}
//依据位置提取相应的数据源对象
String item =dataList.get(position);
//获取用于显示内容的控件的引用
TextView textView = (TextView) rootView.findViewById(R.id.text_view);
//设置显示内容
textView.setText(item);
return rootView;
}
}
}
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:id="@+id/my_spinner"
android:layout_width="225dp"
android:layout_height="wrap_content" />
android:id="@+id/text_view"
style="?android:attr/spinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="marquee"
android:singleLine="true"
android:textAlignment="inherit" />