2015-10-18 34 views
0

我在设计操作栏。我有这个错误You need to use a Theme.AppCompat theme (or descendant) with this activity我知道我应该延长Activity在我的主要活动,但我延长AppCompatActivity因为我使用此代码setSupportActionBar(toolbarTop);如果我删除AppCompatActivity将无法​​为此我将无法使用底部的工具栏;扩展活动错误您需要使用Theme.AppCompat主题

这是我的代码

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> 
+1

张贴AndroidManifest.xml中和你的styles.xml – Blackbelt

+0

现在@Blackbelt检查,请 – Moudiz

+0

更改'CustomActionBarTheme'到'Theme.AppCompat了'parent' .Light.DarkActionBar' – Blackbelt

回答

2

Theme应该像

<style name="CustomActionBarTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 

集父为Theme.AppCompat.Light.DarkActionBar

+0

我得到了这个错误'这个活动已经有一个窗口装饰提供的操作栏。请勿在您的主题中请求Window.FEATURE_SUPPORT_ACTION_BAR并将windowActionBar设置为false以使用工具栏,而不是'我想从我创建的方法中猜测。 – Moudiz

+0

@Moudiz我不明白你的问题是什么? –

+1

无论如何,我的代码中有东西,我调整 – Moudiz

1

更改主题一样

<style name="CustomActionBarTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
    <item name="android:windowActionBar">false</item> 
    <item name="windowActionBar">false</item> 
</style> 

这样你就不会要求动作条,你应该能够使用工具栏,或者更好地利用Theme.AppCompat.Light.NoActionBar,因为要使用ToolBar,你不需要ActionBar。使用Theme.AppCompat.Light.NoActionBar你不需要提供标志windowActionBar你的风格

+0

在你的情况下使用'Theme.AppCompat.Light.NoActionBar'会更好 – Blackbelt

相关问题