我正在尝试将Spinner集成到我的布局中(在AndroidX迁移之前运行良好)。
代码如下:
Spinner spinner_nationalite;
String[] data_nationalite = {"","Française","Suisse","Belge","Allemande"};
@Override
public void onCreate(Bundle savedInstanceState) {
....
spinner_nationalite = (Spinner) findViewById(R.id.update_spinner_nationalite);
ArrayAdapter adapter_nationalite = new ArrayAdapter<String>(this, R.layout.spinner_dropdown_item, data_nationalite);
adapter_nationalite.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinner_nationalite.setAdapter(adapter_nationalite);
spinner_nationalite.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Log.d("KAIROSAPP","Nationalite selected: "+data_nationalite[i]);
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
这是spinner_dropdown_item.xml
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:ellipsize="marquee"
android:fontFamily="sans-serif-thin"
android:textSize="17sp"
android:textStyle="bold"
android:background="#FFFFFF"
android:textColor="#000000"/>
最后是布局XML
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="@+id/update_spinner_nationalite"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView55"/>
我可以看到微调框,但是没有列出任何数据,单击时也没有下拉列表。
我也尝试过使用androidx.appcompat.widget.AppCompatSpinner中的AppCompatSpinner,还使用了android.R.layout.simple_spinner_dropdown_item而不是自定义项,但是我得到了相同的结果。
确保我缺少某些东西。...