我在設計操作欄。我有這個錯誤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>
張貼AndroidManifest.xml中和你的styles.xml – Blackbelt
現在@Blackbelt檢查,請 – Moudiz
更改'CustomActionBarTheme'到'Theme.AppCompat了'parent' .Light.DarkActionBar' – Blackbelt