[英]android:background not working with Theme.MaterialComponents
[英]CreateFromRessource ChipDrawable : The style on this component requires your app theme to be Theme.MaterialComponents
通過使用 AppCompat 並使用 min sdk 版本 21,我嘗試使用 ChipDrawable 在 TextEdit 中的 email 地址列表中使用,並使用以下命令創建該 ChipDrawable:
ChipDrawable chip = ChipDrawable.createFromResource(getApplicationContext(), R.xml.email_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="?colorAccent"
app:closeIconEnabled="true"
app:closeIconTint="@android:color/white" />
我的主題使用Theme.MaterialComponents.DayNight.DarkActionBar
(我也Theme.MaterialComponents.DayNight.DarkActionBar.Bridge
)
但我總是得到這個錯誤:
The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
我嘗試了在 stackoverflow 上找到的所有內容,但沒有太大幫助。
這是我的風格
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.Mareu" parent="Theme.MaterialComponents.DayNight.DarkActionBar.Bridge">
<!-- Primary brand color. -->
<item name="colorPrimary">#4E92DF</item>
<item name="colorPrimaryVariant">#3F7ABD</item>
<item name="colorOnPrimary">@color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">@color/teal_200</item>
<item name="colorSecondaryVariant">@color/teal_700</item>
<item name="colorOnSecondary">@color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
</resources>
這是我畢業的一部分
dependencies {
// Extra
implementation 'org.greenrobot:eventbus:3.1.1'
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.google.android.material:material:1.2.1'
// Compat
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
// TU TI
testImplementation 'org.hamcrest:java-hamcrest:2.0.0.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
問題在這里:
ChipDrawable chip = ChipDrawable.createFromResource(getApplicationContext(),
ApplicationContext
沒有您的應用主題。
您需要傳遞Activity
,而不是 Application Context。
您肯定在您的應用程序中使用 View Binding 並且您正在使用
getLayoutInflater()
嘗試從活動而不是應用程序中獲取充氣機,只需使用
(LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.