你好,我是android的新手,正在学习使用材质芯片。我创建了一个测试项目并添加了以下
添加到我的gradle文件中
implementation 'com.android.support:design:28.0.0'
在我的片段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,但这也不起作用,任何帮助将不胜感激!谢谢!
答案 0 :(得分:2)
正确的是您粘贴的错误:
由于:java.lang.IllegalArgumentException:该组件要求您指定一个有效的TextAppearance属性。更新您的应用程序主题以继承自Theme.MaterialComponents(或后代)。
因此,可以将android:textAppearance="@style/SomeTextAppearance"
添加到您的Chip XML中,或者将样式更改为具有父Theme.MaterialComponents
。
答案 1 :(得分:1)
尝试使用:app:chipText
<android.support.design.chip.Chip
android:id="@+id/some_chip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:chipText="This is a chip" />
不仅如此,还可以将其添加到您的styles.xml
:
parent="Theme.MaterialComponents.Light.NoActionBar"
Read this,之后您可能想做一个干净的重建项目。
com.google.android.material.chip.Chip
实际上是在迁移到AndroidX后工作(如果我没记错的话),因为它来自Material Design。
例如:
<com.google.android.material.chip.Chip
style="@style/TextAppearance.MaterialComponents.Chip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:text="Android"
app:chipIcon="@drawable/androidtag" />
所有这些都应在Android Studio 3.2及更高版本28.0.0上运行,或者在迁移到AndroidX
之后。
答案 2 :(得分:1)
您必须设置AppTheme,以便它继承MaterialComponent主题之一才能使用Chips。如果您仍然想使用AppCompat,请尝试使用Bridge Themes。
在Getting Started with Material Components上遵循本指南中的步骤。
答案 3 :(得分:0)
我们可以使用SELECT
Id,
F1,
F2,
Ref_ID
FROM
@Table
ORDER BY
ISNULL(Ref_ID,ID), ID
主题的Chip支持support.design。
在活动中使用这种风格
MaterialComponent
并在您的活动中创建ChipGroup组件
<style name="ChipStyle" parent="Theme.MaterialComponents.Light.NoActionBar">
<!-- Your style -->
</style>
创建新的xml文件item_filter_chip.xml,即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" />
这行对于充气物品很重要
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" />