其中pre-棒棒糖设备使用appcompat V7工具栏鸵鸟政策工作 [英] Toolbar don´t work with pre-lollipops devices using appcompat v7

查看:129
本文介绍了其中pre-棒棒糖设备使用appcompat V7工具栏鸵鸟政策工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做一个简单的code的工具栏和菜单的抽屉,使用材料appcompatv7。 一切完美的作品上的Nexus 5棒棒糖,但在pre-棒棒糖(4.1或4.4)设备崩溃。 这个问题是在定义的风格。 我把我的code。如果任何人都可以告诉我哪里有故障。

I'm doing a simple code for a toolbar and menu drawer, using material appcompatv7. Everything works perfectly on the Nexus 5 with lollipop, but in a pre-lollipop (4.1 or 4.4) device crashes. The problem comes in defining the style. I put my code if anyone can tell me where the fault is.

这是我的主要活动:

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(我没有风格定义的值,V21)

Style by simply extending Theme.AppCompat.Light.NoActionBar (I have no style defined for values-v21)

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

    </style>
</resources>

这一切都正常工作的设备棒棒糖和pre-棒棒糖。时,要自定义工具栏和状态栏的颜色问题就来了。

This all works correctly on devices lollipop and pre-lollipop. The problem comes when want to customize the colors of the toolbar and status bar

我有这样的变化的值/风格

<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控件是菜单抽屉里(我不明白为什么).. 。但在pre-棒棒糖设备崩溃。 错误抛出我是这样的:

As said earlier, this works fine on the Nexus 5 but although the UI comes with correct colors, I can not see the ListView is inside the Menu Drawer (I do not understand why) ...but in the "pre-lollipop" devices crashes. The error throws me is this:

... java.lang.IllegalStateException:这个活动已经有一个   由窗口装饰提供的操作栏。不要求   Window.FEATURE_ACTION_BAR并设置windowActionBar为false您   主题使用工具栏代替。

...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风格中的项目是假的主要活动已成为工具栏到android.support.v7.widget.Toolbar工具栏,但没有成功 ... 我也搜索已经给出的例子,但不是为我工作 我使用的Eclipse,manifiest 21的目标,最小的16岁,我还更新了SDK和ADT ...

I searched a lot of information about this error and tried many options: the item windowActionBar within the style is "false" in the main activity have become "Toolbar" to "android.support.v7.widget.Toolbar toolbar" but without success ... I have also searched for examples already given, but not working for me I'm using Eclipse, the target of manifiest 21 and the minimum is 16, I have also updated sdk and adt ...

谁能帮助在设备上pre-棒棒糖正常工作?

Can anyone help to work correctly on devices pre-lollipop?

推荐答案

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

要叫 setSupportActionBar ,你不能再有一个操作栏。这就是为什么你得到

To call setSupportActionBar you can't have another action bar. That's why you get

该活动已经有靠窗户的装饰提供一个操作栏

This Activity already has an action bar supplied by the window decor

请确保你的主题没有一个动作条使用工具栏。

Make sure your theme doesn't have an actionbar to use toolbar.

这篇关于其中pre-棒棒糖设备使用appcompat V7工具栏鸵鸟政策工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆