2015-10-18 38 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

相關問題