工具栏不适用于使用appcompat v7的pre-lollipops设备

Ser*_*o76 5 android toolbar android-5.0-lollipop

我正在使用材料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);
    }
}
Run Code Online (Sandbox Code Playgroud)

主要布局:

<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>
Run Code Online (Sandbox Code Playgroud)

简单地扩展Theme.AppCompat.Light.NoActionBar的样式(我没有为values-v21定义样式)

<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

    </style>
</resources>
Run Code Online (Sandbox Code Playgroud)

这一切都适用于设备棒棒糖和前棒棒糖.想要自定义工具栏和状态栏的颜色时出现问题

我在值/样式中进行了此更改

<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>
Run Code Online (Sandbox Code Playgroud)

我将添加到值-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>
Run Code Online (Sandbox Code Playgroud)

如前所述,这在Nexus 5上工作得很好但是虽然UI带有正确的颜色,我看不到ListView在菜单抽屉里面(我不明白为什么).. 但在"pre-lollipop"设备中崩溃. 错误抛出我是这样的:

... java.lang.IllegalStateException:此Activity已经有一个由窗口装饰提供的操作栏.不要在主题中请求Window.FEATURE_ACTION_BAR并将windowActionBar设置为false以使用工具栏.

我搜索了很多关于这个错误的信息,并尝试了很多选项:样式中的windowActionBar在主要活动中的"假"是"工具栏"到"android.support.v7.widget.Toolbar工具栏"但没有成功. ..我也搜索了已经给出的例子,但没有为我工作我正在使用Eclipse,最明显的目标是21,最小值是16,我还更新了sdk和adt ...

任何人都可以帮助在棒棒糖前的设备上正常工作吗?

Ped*_*ira 14

Theme.AppCompat.Light.NoActionBar改用.

打电话给 setSupportActionBar你不能有另一个动作栏.这就是你得到的原因

此活动已经有一个由窗口装饰提供的操作栏

确保您的主题没有使用工具栏的操作栏.