动态向芯片组添加芯片

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

Here is the screenshot

  1. activity_main.xml

        <com.google.android.material.chip.ChipGroup
            android:id="@+id/chip_group_main"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="2dp"
            app:chipSpacingHorizontal="4dp">
    
            <com.google.android.material.chip.Chip
                style="@style/ChipTextAppearance"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello"
                app:chipBackgroundColor="@color/colorAccent"
                app:closeIconEnabled="true" />
    
            <com.google.android.material.chip.Chip
                style="@style/ChipTextAppearance"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="World"
                app:chipBackgroundColor="@color/colorAccent"
                app:closeIconEnabled="true" />
    
    
        </com.google.android.material.chip.ChipGroup>
    </HorizontalScrollView>
    
  2. in 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);
    
  3. 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>
    
  4. 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" />```
    
评论