![](/img/trans.png)
[英]Error Message: You need to use a Theme.AppCompat theme with this activity
[英]error in extend activity You need to use a Theme.AppCompat theme
我正在设计动作栏。 我遇到此错误You need to use a Theme.AppCompat theme (or descendant) with this activity
我知道我应该在主活动中扩展Activity
,但是我在扩展AppCompatActivity
因为我正在使用此代码setSupportActionBar(toolbarTop);
如果我删除了AppCompatActivity
将无法正常工作,我将无法使用底部的工具栏;
这是我的代码
public class MainActivity extends AppCompatActivity {
...
private void initToolbars() {
Toolbar toolbarTop = (Toolbar) findViewById(R.id.toolbar_top);
setSupportActionBar(toolbarTop);
Toolbar toolbarBottom = (Toolbar) findViewById(R.id.drawer_layout);
toolbarBottom.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
// Single menu item is selected do something
// Ex: launching new activity/screen or show alert message
Toast.makeText(MainActivity.this, "Bookmark is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_save:
Toast.makeText(MainActivity.this, "Save is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_search:
Toast.makeText(MainActivity.this, "Search is Selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_share:
Toast.makeText(MainActivity.this, "Share is Selected", Toast.LENGTH_SHORT).show();
return true;
}
return true;
}
});
toolbarBottom.inflateMenu(R.menu.menu_main);
}
表现
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.je.www.i" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/CustomActionBarTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
样式
<resources>
<!-- the theme applied to the application or activity -->
<style name="CustomActionBarTheme"
parent="@android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">@style/MyActionBar</item>
</style>
<!-- ActionBar styles -->
<style name="MyActionBar"
parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#FF0000</item>
</style>
</resources>
您的Theme
应该像
<style name="CustomActionBarTheme" parent="Theme.AppCompat.Light.DarkActionBar">
将父Theme.AppCompat.Light.DarkActionBar
设置为Theme.AppCompat.Light.DarkActionBar
像这样改变你的主题
<style name="CustomActionBarTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="android:windowActionBar">false</item>
<item name="windowActionBar">false</item>
</style>
这样,您将不会请求ActionBar,并且应该能够使用工具栏,或者更好地使用Theme.AppCompat.Light.NoActionBar
,因为您要使用ToolBar
并且不需要ActionBar
。 使用Theme.AppCompat.Light.NoActionBar
您不需要为样式提供标志windowActionBar
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.