简体   繁体   English

如何同时使用android:Theme.Material和工具栏?

[英]How to use android:Theme.Material and Toolbar together?

I am creating a android application where I am using Material Design components.I have used Material design theme(android:Theme.Material ), by extending my style with android:Theme.Material . 通过与Android延长我的风格:我创造,我现在用的材料设计components.I已使用材料的设计主题(Theme.Material机器人):一个Android应用程序Theme.Material。

<style name="MyMaterialTheme.Base" parent="android:Theme.Material.Light">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
    <item name="android:navigationBarColor">@color/navigationBarColor</item>
    <item name="android:displayOptions">disableHome</item>
</style>

Now, the issue is that I want to use toolbar also, but as I followed many tutorial I need to have Theme.AppCompat as my parent theme, if I want to use Toolbar. 现在,问题是我也想使用工具栏,但是当我遵循许多教程时,如果要使用工具栏,则需要将Theme.AppCompat作为父主题。

I tried with Material Design Theme, but throwing me the error as 我尝试了“材料设计主题”,但将错误抛出为

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity

So, my question is, I can't use Material Design theme with Toolbar(as I am using Card Views also, so Material Design Theme is preferable) ? 所以,我的问题是,我不能在工具栏上使用Material Design主题(因为我也使用Card Views,因此Material Design Theme是更好的选择)?

Note:-I haveintegrated Toolbar using AppCompact theme, but I want to use Material Design theme only. 注意:-我已经使用AppCompact主题集成了工具栏,但是我只想使用Material Design主题。

将parent设置为“ @android:style / Theme.Material.Light.NoActionBar”,并在活动中使用AppCompatActivity。

You're getting 你正在

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity

because your Activity extends from AppCompatActivity and to use it is mandatory that your activity theme must have a Theme.AppCompat or descendant. 因为您的ActivityAppCompatActivity扩展而来的,所以必须使用活动主题必须具有Theme.AppCompat或后代。

You could use Toolbar in your activity without extending from AppCompatActivity like this post refer. 你可以使用Toolbar在你的活动,而从扩展AppCompatActivity喜欢这个岗位参考。 But you still need a Theme.AppCompat descendant. 但是您仍然需要Theme.AppCompat后代。

BTW. BTW。 Why do you need specifically android:Theme.Material ? 为什么您特别需要android:Theme.Material

You need to update the theme in your manifest. 您需要更新清单中的主题。

Edit your theme entry to look like this: 编辑主题条目,如下所示:

android:theme="@style/Theme.AppCompat"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Theme.material中使用TextInputLayout? - How to use TextInputLayout with Theme.material? 如何在Theme.Material中使用ActionBarActivity - How to use ActionBarActivity with Theme.Material 如何在styles.xml android中使用android:Theme.Material(Material theme)? - How to use android:Theme.Material (Material theme) in styles.xml android? Android L Theme.Material无法识别 - Android L Theme.Material not being recognized 在android studio中找不到“Theme.Material” - Cannot find “Theme.Material” in android studio 无法将ActionBarActivity与Theme.Material一起使用 - Can't use ActionBarActivity with Theme.Material 带有Theme.Material的Android设计支持库 - Android design support library with Theme.Material 将 Theme.Material 用于 API21 及更高版本,并在带有工具栏的 Activity 中保持兼容性 - Use Theme.Material for API21 and above and maintaining compatibility in an Activity with a Toolbar Android 中的 Theme.AppCompat 和 Theme.Material 有什么区别? - What is the difference between Theme.AppCompat and Theme.Material in Android? 错误:获取AppCompatActivity与android:Theme.Material不兼容 - Error: Getting AppCompatActivity is not compatible with android:Theme.Material
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM