嵌套旋转器Android设计问题



我是第一次使用微调器,我不明白为什么第二个微调器看起来不完全像第一个,尽管它们实际上是相同的(唯一的区别是数据)。

设计用"约束布局"实现

<Spinner
android:id="@+id/spCategorie"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="@+id/categorieText"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/categorieText"
android:layout_width="73dp"
android:layout_height="28dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="Categorie:"
android:textAlignment="viewStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="@+id/spProduct"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/productText"
app:layout_constraintTop_toBottomOf="@+id/spCategorie" />

这是它在模拟器

中的样子
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, productsCat);
arrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
categorie.setAdapter(arrayAdapter);
categorie.setOnItemSelectedListener(new SpinnersEvents());
product.setOnItemSelectedListener(new SpinnersEvents());
private class SpinnersEvents implements AdapterView.OnItemSelectedListener {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.spCategorie){
String[] productsName = getProductName(productsCat[position]);
ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(getBaseContext(),R.layout.support_simple_spinner_dropdown_item, productsName);
arrayAdapterChild.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
product.setAdapter(arrayAdapterChild);
}else{
price.setText(String.valueOf(tempList.get(position).getPrice()));
imgPrd.setImageResource(tempList.get(position).getImage());
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}

请在您的代码中尝试这可能会对您有所帮助,您也可以在旋转器中设置自动换行内容或增加大小以查看文本

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="MissingConstraints">
<TextView
android:id="@+id/categorieText"
android:layout_width="73dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Categorie:"
android:textAlignment="viewStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="RtlCompat" />
<Spinner
android:id="@+id/spCategorie"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@+id/categorieText"/>

<TextView
android:id="@+id/productText"
android:layout_width="73dp"
android:layout_height="28dp"
android:layout_marginTop="32dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:text="Product:"
android:textAlignment="viewStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/categorieText"/>
<Spinner
android:id="@+id/spproduct"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginTop="32dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintTop_toBottomOf="@+id/spCategorie"
app:layout_constraintStart_toEndOf="@+id/categorieText"/>
</androidx.constraintlayout.widget.ConstraintLayout>

解决方案在代码中,而不是在布局中。

第二个转轮必须是:

ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item, productsName);

:

ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(getBaseContext(),R.layout.support_simple_spinner_dropdown_item, productsName);

在本例中,调用getBaseContext()方法会产生特定的问题。

很抱歉造成混乱。

最新更新