我正在尝试使用AppCompact旁边的材料组件,所以我确实遵循了
1-我为AppCompact创建了样式父级,并为我的全局主题将其命名为AppTheme
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:fontFamily">@font/iranyekanwebregular</item>
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>
2-根据这篇(https://material.io/develop/android/docs/getting-started/)文章,我在TextInputLayout中使用outLineBox样式制作了一个TextInputEditText,这正是上述文章所说的。
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/til_hive_desc"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:inputType="text"
android:textColorHint="@color/colorPrimaryDark">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/tied_movement_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="@string/add_apiary_desc"
android:inputType="text"
android:textColor="@color/colorPrimaryDark"
android:textColorHint="@color/colorPrimaryDark"
android:textSize="14sp" />
</com.google.android.material.textfield.TextInputLayout>
,但是它不起作用,当我尝试打开活动时它崩溃了,这是错误消息:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.soroushkh.mybees, PID: 22912
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.soroushkh.mybees/com.soroushkh.mybees.AddApiaryActivity}: android.view.InflateException: Binary XML file line #33: Binary XML file line #33: Error inflating class com.google.android.material.textfield.TextInputLayout
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: android.view.InflateException: Binary XML file line #33: Binary XML file line #33:
Error inflating class com.google.android.material.textfield.TextInputLayout
Caused by: android.view.InflateException: Binary XML file line #33: Error inflating class
com.google.android.material.textfield.TextInputLayout
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
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.rInflate(LayoutInflater.java:866)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
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 android.view.LayoutInflater.inflate(LayoutInflater.java:374)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.soroushkh.mybees.AddApiaryActivity.onCreate(AddApiaryActivity.java:24)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at
android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
E/AndroidRuntime: Caused by: java.lang.IllegalArgumentException: The style on this component
requires your app theme to be Theme.MaterialComponents (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:243)
at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:217)
at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:145)
at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:115)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:451)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:410)
,它要我提供Theme.MaterialComponents,当我实现它时,它破坏了我所有的自定义设计。 我该怎么办,请回答。
答案 0 :(得分:1)
将样式父样式更改为材料样式,然后查看它是否起作用?
<style name="AppTheme" parent="parent="Theme.MaterialComponents.Light.DarkActionBar"">
.
</style>
答案 1 :(得分:0)
通过将应用主题更改为继承来进行应用范围内的迁移 建议使用“材料组件”主题。但是, 一定要在以后进行彻底测试,作为现有布局中的组件 可能会改变他们的容貌和行为。
我尝试了相同的代码,但崩溃了
更改后
Theme.AppCompat.Light.DarkActionBar
收件人
Theme.MaterialComponents.Light.DarkActionBar
它正在运行,如果您想设置自定义样式,则可以在片段/活动中添加activity!!.setTheme(R.style.textTheme)
并应用主题
希望它对您有用。