动态添加芯片到芯片组

Posted

技术标签:

【中文标题】动态添加芯片到芯片组【英文标题】:Dynamically add chips to chipgroup 【发布时间】:2020-09-02 22:42:41 【问题描述】:

我正在尝试将几个芯片动态添加到芯片组中。第一个看起来不错,但其他看起来不正确。但是当我使用 XML 时,它工作正常。

activity_main.xml

<HorizontalScrollView
            android:layout_
            android:layout_
            android:scrollbars="none"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toStartOf="@+id/chips_select"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView"
            app:layout_constraintVertical_bias="0.51">
    <com.google.android.material.chip.ChipGroup
                android:id="@+id/chip_group_main"
                android:layout_
                android:layout_
                android:layout_marginStart="8dp"
                android:layout_marginTop="2dp"
                app:chipSpacingHorizontal="4dp">
        <com.google.android.material.chip.Chip
                    style="@style/ChipTextAppearance"
                    android:layout_
                    android:layout_
                    android:text="Hello"
                    app:chipBackgroundColor="@color/colorAccent"
                    app:closeIconEnabled="true" />
        <com.google.android.material.chip.Chip
                    style="@style/ChipTextAppearance"
                    android:layout_
                    android:layout_
                    android:text="World"
                    app:chipBackgroundColor="@color/colorAccent"
                    app:closeIconEnabled="true" />
    </com.google.android.material.chip.ChipGroup>
</HorizontalScrollView>

在 MainActivity.java 中

@Override
protected void onCreate(Bundle savedInstanceState) 
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  Chip chip = new Chip(this);
  chip.setText("ABC");
  chip.setChipBackgroundColorResource(R.color.colorAccent);
  chip.setCloseIconVisible(true);
  chip.setTextColor(getResources().getColor(R.color.white));
  chip.setTextAppearance(R.style.ChipTextAppearance);

  Chip chip2 = new Chip(this);
  chip.setText("XYZ");
  chip.setChipBackgroundColorResource(R.color.colorAccent);
  chip.setCloseIconVisible(true);
  chip.setTextColor(getResources().getColor(R.color.white));
  chip.setTextAppearance(R.style.ChipTextAppearance);

  ChipGroup chipGroup = findViewById(R.id.chip_group_main);

  chipGroup.addView(chip);
  chipGroup.addView(chip2);

style.xml

<resources>
    <style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="ChipTextAppearance" parent="TextAppearance.MaterialComponents.Chip">
        <item name="android:textColor">@android:color/white</item>
        <item name="android:textSize">12sp</item>
    </style>
</resources>

standalone_chip.xml

<?xml version="1.0" encoding="utf-8"?>
<chip
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:chipBackgroundColor="@color/colorAccent"
    app:closeIconEnabled="true"
    style="@style/ChipTextAppearance"
    app:closeIconTint="@android:color/white" />

【问题讨论】:

【参考方案1】:

在您的主要活动中,您正在这样做。

@Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Chip chip = new Chip(this);
        chip.setText("ABC");
        chip.setChipBackgroundColorResource(R.color.colorAccent);
        chip.setCloseIconVisible(true);
        chip.setTextColor(getResources().getColor(R.color.white));
        chip.setTextAppearance(R.style.ChipTextAppearance);

        Chip chip2 = new Chip(this);
        chip.setText("XYZ");
        chip.setChipBackgroundColorResource(R.color.colorAccent);
        chip.setCloseIconVisible(true);
        chip.setTextColor(getResources().getColor(R.color.white));
        chip.setTextAppearance(R.style.ChipTextAppearance);


        ChipGroup chipGroup = findViewById(R.id.chip_group_main);

        chipGroup.addView(chip);
        chipGroup.addView(chip2);

请注意,您正在创建一个新的芯片实例,即芯片 2,但您在下一行中更改的是芯片而不是芯片 2 实例,因此芯片 2 不会受到您之前所做的任何更改的影响。因此,为了解决这个问题,您可以像这样更改您的代码

@Override
        protected void onCreate(Bundle savedInstanceState) 
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);


            Chip chip = new Chip(this);
            chip.setText("ABC");
            chip.setChipBackgroundColorResource(R.color.colorAccent);
            chip.setCloseIconVisible(true);
            chip.setTextColor(getResources().getColor(R.color.white));
            chip.setTextAppearance(R.style.ChipTextAppearance);

            Chip chip2 = new Chip(this);
            chip2.setText("XYZ");  //chip2
            chip2.setChipBackgroundColorResource(R.color.colorAccent);
            chip2.setCloseIconVisible(true);
            chip2.setTextColor(getResources().getColor(R.color.white));
            chip2.setTextAppearance(R.style.ChipTextAppearance);


            ChipGroup chipGroup = findViewById(R.id.chip_group_main);

            chipGroup.addView(chip);
            chipGroup.addView(chip2);

【讨论】:

【参考方案2】:

是的,可以使用 chipGroup.addView(chip); 将筹码添加到组中 请注意,您应该为每个芯片添加 id 以便完美地完成某些组工作的功能(例如singleSelection="true"

 chip.setId(ViewCompat.generateViewId());

【讨论】:

以上是关于动态添加芯片到芯片组的主要内容,如果未能解决你的问题,请参考以下文章

在 Angular 中,芯片的动态颜色

颤振多选可搜索下拉菜单,将所选项目作为芯片添加到字段中

高通发表动态调整降噪效果的耳机设计方案 无须特别更改耳塞尺寸

安全芯片的安全特性

stm32怎么添加芯片包无响应

为啥keil官网下载芯片包解压后没有芯片