gpt4 book ai didi

java - 动态添加芯片到芯片组

转载 作者:行者123 更新时间:2023-12-04 23:37:11 32 4
gpt4 key购买 nike

我正在尝试动态地将几个芯片添加到芯片组中。第一个看起来不错,但其他的看起来不正确。但是当我使用 XML 时它工作正常。
The last chip added is small, grey, and has no text. It should be orange and contain text like the first three.
activity_main.xml

<HorizontalScrollView
android:layout_width="350dp"
android:layout_height="wrap_content"
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_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>
在 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);
}
样式.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>
独立芯片.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" />

最佳答案

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

@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);

关于java - 动态添加芯片到芯片组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61847481/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com