工具栏在使用appcompat v7时无法与早期版本的设备(低于Android Lollipop)兼容。

5

我正在编写一个工具栏和菜单抽屉的简单代码,使用材料设计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>

这在Lollipop及其之前的设备上都能正常工作。当想要自定义工具栏和状态栏的颜色时,问题就来了。
我在values/styles中进行了更改。
<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>

我为values-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 在菜单抽屉(我不知道为什么)中.. 但在“棒棒糖”(即 Android 5.0)之前的设备上会崩溃。 报错信息如下:

…java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.

我搜索了很多关于此错误的信息并尝试了许多选项:样式中的 windowActionBar 属性已设置为“false”,主活动已改为使用“Toolbar”替换为“android.support.v7.widget.Toolbar toolbar”,但均未成功... 我还查找了其他示例,但对我没有用。 我正在使用 Eclipse,在清单文件中的 target 版本为 21,最低版本为 16,我也已更新了 SDK 和 ADT...

有谁能帮忙让它在棒棒糖之前的设备上正常工作吗?


你是否得到了正确的答案? - Sauron
1个回答

14

使用Theme.AppCompat.Light.NoActionBar替代。

要调用setSupportActionBar,您不能有另一个操作栏。这就是为什么会出现以下错误提示:

该Activity已经具有由窗口装饰提供的操作栏

请确保您的主题没有操作栏以使用工具栏。


工作中!谢谢大家 ;) - Sergio76

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接