gpt4 book ai didi

android - 使用安卓 Material 芯片

转载 作者:太空宇宙 更新时间:2023-11-03 12:26:29 26 4
gpt4 key购买 nike

您好,我是android新手,正在学习使用material chips。我创建了一个测试项目并添加了以下内容

我添加到我的 gradle 文件中

implementation 'com.android.support:design:28.0.0'

并在我添加的 fragment xml 中

<android.support.design.chip.Chip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="test chip"/>

但我一直收到这个错误

 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: Binary XML file line #20: Error inflating class android.support.design.chip.Chip
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
at android.view.LayoutInflater.createView(LayoutInflater.java:647)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:863)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at nonso.android.test.ui.main.MainFragment.onCreateView(MainFragment.java:26)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3269)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3229)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:620)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)
at android.app.Activity.performStart(Activity.java:7029)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2741)
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: java.lang.IllegalArgumentException: This component requires that you specify a valid TextAppearance attribute. Update your app theme to inherit from Theme.MaterialComponents (or a descendant).
at android.support.design.internal.ThemeEnforcement.checkTextAppearance(ThemeEnforcement.java:170)
at android.support.design.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:75)
E/AndroidRuntime: at android.support.design.chip.ChipDrawable.loadFromAttributes(ChipDrawable.java:343)
at android.support.design.chip.ChipDrawable.createFromAttributes(ChipDrawable.java:278)
at android.support.design.chip.Chip.<init>(Chip.java:172)
at android.support.design.chip.Chip.<init>(Chip.java:165)
... 31 more

似乎 android.support.design.chip.Chip 有问题,我尝试使用 com.google.android.material.chip.Chip 但这也不起作用,任何帮助将不胜感激!谢谢!

最佳答案

我们可以通过 MaterialComponent 主题为 Chip 使用 support.design。

在你的 Activity 中使用这种风格

<style name="ChipStyle" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Your style -->
</style>

并在您的 Activity 中创建 ChipGroup 组件

 <android.support.design.chip.ChipGroup
android:id="@+id/cg_filter_items"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="16dp"
android:padding="16dp"
app:chipSpacing="8dp"
app:layout_constraintBottom_toTopOf="@id/btn_apply_filter"
app:layout_constraintTop_toBottomOf="@id/tv_filter_heading" />

创建新的 xml 文件 item_filter_chip.xml 即 ChipGroup 的项目style="@style/TextAppearance.MaterialComponents.Chip"这一行对于给项目充气很重要

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.chip.Chip
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/chips_item_filter"
style="@style/TextAppearance.MaterialComponents.Chip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp" />

使用这段代码动态生成筹码

    ChipGroup chipGroup = findViewById(R.id.cg_filter_items);
for (int i = 0; i < 10; i++) {
View view = LayoutInflater.from(this).inflate(R.layout.item_filter_chips, chipGroup, false);
Chip chip = view.findViewById(R.id.chips_item_filter);
chip.setText("chip Text " + i);
chip.setClickable(true);
chip.setCheckable(true);
chipGroup.addView(chip);
chip.setOnClickListener(<Your Listener>);
}

关于android - 使用安卓 Material 芯片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52526670/

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