【问题标题】:Nested Spinner Android Design Issues ConstraintLayout嵌套微调器 Android 设计问题 ConstraintLayout
【发布时间】:2021-10-04 23:36:03
【问题描述】:

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

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

<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" />

This is how it looks in the emulator

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

    }
}

【问题讨论】:

    标签: java android android-constraintlayout


    【解决方案1】:

    请在您的代码中尝试这个可能对您有所帮助,您还可以在微调器中设置 wrapcontent 或增加大小以正确查看文本

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

    【讨论】:

      【解决方案2】:

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

      第二个微调器必须是:

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

      很抱歉给您带来了困惑。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-26
        • 1970-01-01
        • 2020-02-22
        • 2020-05-12
        • 1970-01-01
        相关资源
        最近更新 更多