首页 > 解决方案 > 嵌套微调器 Android 设计问题 ConstraintLayout

问题描述

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

该设计使用“约束布局”实现它

<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) {

    }
}

标签: javaandroidandroid-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() 方法会产生该特定问题。

对困惑感到抱歉。


推荐阅读