我正在使用Material appcompatv7为工具栏和菜单抽屉做一个简单的代码。在带有棒棒糖的Nexus 5上一切正常,但在棒棒糖(4.1或4.4)之前的设备上崩溃。问题在于定义样式。如果有人能告诉我故障在哪里,我会放我的代码。
这是我的主要活动:
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Hello extends ActionBarActivity {
private ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
toggle = new ActionBarDrawerToggle(
this,
drawerLayout,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
toggle.setDrawerIndicatorEnabled(true);
drawerLayout.setDrawerListener(toggle);
ListView lv_navigation_drawer = (ListView) findViewById(R.id.lv_navigation_drawer);
lv_navigation_drawer.setAdapter(new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
new String[] {"Screen 1", "Screen 2", "Screen 3"}));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (toggle.onOptionsItemSelected(item))
return true;
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
toggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
toggle.onConfigurationChanged(newConfig);
}
}
主要布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
theme="@style/ThemeOverlay.AppCompat.ActionBar"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize" />
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/lv_navigation_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="@android:color/white" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
通过简单地扩展Theme.AppCompat.Light.NoActionBar来设置样式(我没有为values-v21定义样式)
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
</resources>
这一切都在棒棒糖和棒棒糖前设备上正常工作。当想要自定义工具栏和状态栏的颜色时,问题就来了
我在价值观/样式上进行了此更改
<resources>
<style name="AppTheme" parent="Base.Theme.AppCompat"/>
<style name="AppTheme.Base" parent="Theme.AppCompat">
<item name="colorPrimary">#2ecc71</item>
<item name="colorPrimaryDark">#27ae60</item>
<item name="android:windowNoTitle">true</item>
<item name="windowActionBar">false</item>
<item name="drawerArrowStyle">@style/DrawerArrowStyle</item>
</style>
<style name="DrawerArrowStyle" parent="Widget.AppCompat.DrawerArrowToggle">
<item name="spinBars">true</item>
<item name="color">@android:color/white</item>
</style>
<color name="primary">#457C50</color>
<color name="primaryDarker">#580C0C</color>
</resources>
我为值添加样式-v21
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppTheme" parent="AppTheme.Base">
<item name="android:windowContentTransitions">true</item>
<item name="android:windowAllowEnterTransitionOverlap">true</item>
<item name="android:windowAllowReturnTransitionOverlap">true</item>
<item name="android:windowSharedElementEnterTransition">@android:transition/move</item>
<item name="android:windowSharedElementExitTransition">@android:transition/move</item>
</style>
</resources>
如前所述,这在 Nexus 5 上运行良好,但尽管 UI 带有正确的颜色,但我看不到 ListView 在菜单抽屉内(我不明白为什么)。但是在"棒棒糖前"设备崩溃。抛出我的错误是这样的:
。java.lang.IllegalStateException:此活动已经有一个 操作栏由窗户装饰提供。不请求 Window.FEATURE_ACTION_BAR并将窗口操作栏设置为 false 主题以改用工具栏。
我搜索了很多关于此错误的信息并尝试了许多选项:样式中的项目窗口操作栏在主活动中为"false"已变为"工具栏"到"android.support.v7.widget.工具栏工具栏"但没有成功...我也搜索了已经给出的例子,但对我不起作用我正在使用 Eclipse,目标是 21 个,最小值是 16 个,我还更新了 sdk 和 adt ...
任何人都可以帮助在棒棒糖之前的设备上正常工作吗?
改用Theme.AppCompat.Light.NoActionBar
。
若要调用setSupportActionBar
不能有其他操作栏。这就是为什么你会得到
此活动已具有由窗口装饰提供的操作栏
确保您的主题没有用于使用工具栏的操作栏。