在我正在开发的应用程序的一个片段中,我让用户创建各种芯片,每个芯片代表一个选项。我能够为芯片创建动画。
现在,当用户点击芯片时,我将其从组中删除。我能够将自定义动画与删除相关联(参见 gif),但是当删除“中间筹码”时,右侧的筹码会在动画结束时突然向左移动。
布局:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="@dimen/horizontal_scroll_height"
android:scrollbars="none">
<com.google.android.material.chip.ChipGroup
android:id="@+id/rouletteChipList"
style="@style/Widget.MaterialComponents.ChipGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="@dimen/chip_horizontal_margin"
android:paddingEnd="@dimen/chip_horizontal_margin"
app:chipSpacing="@dimen/chip_spacing"
app:singleLine="true">
</com.google.android.material.chip.ChipGroup>
</HorizontalScrollView>
Run Code Online (Sandbox Code Playgroud)
芯片删除:
private void removeChip(final Chip chip) {
@SuppressWarnings("ConstantConditions") final ChipGroup optionsList = getView().findViewById(R.id.ChipList);
// Remove the chip with an animation
if (chip == null) return;
final Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.chip_exit_anim);
chip.startAnimation(animation);
chip.postDelayed(new Runnable() {
@Override
public void run() {
optionsList.removeView(chip);
}
}, 400);
}
Run Code Online (Sandbox Code Playgroud)
芯片布局:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.chip.Chip xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content" …Run Code Online (Sandbox Code Playgroud) 我在我的布局中使用芯片视图
自从材料设计组件从1.0.0升级到1.1.0 后,视图的顶部和底部有一个额外的空间
我找不到任何关于如何删除这些空间的文档
在材料 1.0.0
在材料 1.1.0
<com.google.android.material.chip.Chip
style="@style/Widget.MaterialComponents.Chip.Action"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:text="Groceries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Run Code Online (Sandbox Code Playgroud) android android-view material-design android-chips material-components-android
我已经阅读了官方网站上的文档。但是,根据文档,我无法在我的项目中实现Chip。Android Studio无法找到和导入Chip类以及Chip视图。
我还注意到,在Google Developer网站上没有Chip类的参考。
还有一些类似的问题。但是所有答案都指向使用第三方库。但我试图使用android支持库中的Chips组件。
我想创建 ChipGroup 2 路绑定适配器。我复制了默认的 RadioGroup 绑定适配器并进行了一些更改,但它不适用于两种方式。如果以编程方式将数据设置为 observable,ChipGroup 会从中检索更改。但是手动芯片选择不会将更改设置为可观察的。
这是我的适配器
@InverseBindingMethods(InverseBindingMethod(type = ChipGroup::class, attribute = "android:checkedButton", method = "getCheckedRadioButtonId"))
class ChipGroupBindingAdapter {
companion object {
@JvmStatic
@BindingAdapter("android:checkedButton")
fun setCheckedChip(view: ChipGroup?, id: Int) {
if (id != view?.checkedChipId) {
view?.check(id)
}
}
@JvmStatic
@BindingAdapter(value = ["android:onCheckedChanged", "android:checkedButtonAttrChanged"], requireAll = false)
fun setChipsListeners(view: ChipGroup?, listener: ChipGroup.OnCheckedChangeListener?,
attrChange: InverseBindingListener?) {
if (attrChange == null) {
view?.setOnCheckedChangeListener(listener)
} else {
view?.setOnCheckedChangeListener { group, checkedId ->
listener?.onCheckedChanged(group, checkedId)
attrChange.onChange()
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
布局文件:
<android.support.design.chip.ChipGroup …Run Code Online (Sandbox Code Playgroud) 您好,我是 android 新手,正在学习使用材料芯片。我创建了一个测试项目并添加了以下内容
我添加到我的 gradle 文件中
implementation 'com.android.support:design:28.0.0'
Run Code Online (Sandbox Code Playgroud)
在我的片段 xml 中我添加了
<android.support.design.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test chip"/>
Run Code Online (Sandbox Code Playgroud)
但我一直收到这个错误
java.lang.RuntimeException: Unable to start activity ComponentInfo{nonso.android.test/nonso.android.test.MainActivity}: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class android.support.design.chip.Chip
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.view.InflateException: Binary XML file line #20: Binary XML file line #20: Error inflating class android.support.design.chip.Chip
Caused by: android.view.InflateException: …Run Code Online (Sandbox Code Playgroud) 我需要使用材料设计库以及将通过 API 调用检索的图标动态添加入口芯片。如何使用 Glide 库设置芯片图标?
注意:我之前没有在 drawable 文件夹中保存任何图像。所有数据都是通过 API 调用检索的。
我有以下芯片组,当我点击已经选中的芯片时,它会被取消选中,但我必须始终有一个选中的芯片。行为类似于无线电组。
<com.google.android.material.chip.ChipGroup
android:id="@+id/chip_group_filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:checkedChip="@id/chip_1"
app:singleSelection="true">
<com.google.android.material.chip.Chip
android:id="@+id/chip_1"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chip1"
app:chipBackgroundColor="@color/chip_color" />
<com.google.android.material.chip.Chip
android:id="@+id/chip_2"
style="@style/Widget.MaterialComponents.Chip.Choice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/chip2"
app:chipBackgroundColor="@color/chip_color" />
</com.google.android.material.chip.ChipGroup>
Run Code Online (Sandbox Code Playgroud) android android-chips material-components material-components-android
我想用 alpha 更改 com.google.android.material.chip.Chip 背景,但是当我设置时
app:chipBackgroundColor="#6F000000"
Run Code Online (Sandbox Code Playgroud)
这不起作用并且总是有白色背景。我怎样才能做到这一点?
我正在尝试将几个芯片动态添加到芯片组中。第一个看起来不错,但其他的显示不正确。但是当我使用 XML 时它工作正常。请帮忙。
活动_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>
Run Code Online (Sandbox Code Playgroud)在 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);
Run Code Online (Sandbox Code Playgroud)样式文件
<resources>
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item> …Run Code Online (Sandbox Code Playgroud)在我的 XML 中,我只是声明 aChipGroup如下:
<com.google.android.material.chip.ChipGroup
android:id="@+id/chipGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Run Code Online (Sandbox Code Playgroud)
然后Chip动态添加每个(其中CustomChipFilterStyle将它们设置为“过滤器”类型Chip):
ChipGroup chipGroup = findViewById(R.id.chipGroup);
for (String name : names) {
Chip chip = new Chip(this, null, R.attr.CustomChipFilterStyle);
chip.setText(name);
chipGroup.addView(chip);
}
Run Code Online (Sandbox Code Playgroud)
在指南中(请参阅“可移动”下的视频剪辑),它建议“输入芯片可以重新排序或移动到其他字段”:
但我看不到任何关于如何完成此操作的指导,也找不到任何示例。它是完全定制的东西(通过View.OnDragListener和chip.setOnDragListener()),还是作为Chip框架的一部分有实用方法?我真正需要做的就是Chip在同一个ChipGroup. 我确实开始了,chip.setOnDragListener()但很快意识到我没有足够的知识来创建必要的动画来推动和重新排序其他ChipsChip本身被拖动(以及区分点击 - 过滤 - 和拖)...我希望可能有一些开箱即用的方法来做到这一点,ChipGroup就像上面的指南中可能提到的那样。
android ×10
android-chips ×10
java ×2
material-components-android ×2
xml ×2
android-view ×1
androidx ×1
kotlin ×1
material ×1