【问题标题】:Toolbar don´t work with pre-lollipops devices using appcompat v7工具栏不适用于使用 appcompat v7 的预棒棒糖设备
【发布时间】:2015-01-14 07:40:46
【问题描述】:

我正在使用材料 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>

我为 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 在 Menu Drawer 内(我不明白为什么)...但是在“pre-lollipop”设备崩溃。 我抛出的错误是这样的:

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

我搜索了很多有关此错误的信息并尝试了很多选项:主活动中样式为“false”的项目windowActionBar已变为“工具栏”到“android.support.v7.widget.Toolbar工具栏”但是没有成功 ... 我还搜索了已经给出的示例,但不适用于我 我正在使用 Eclipse,manifiest 的目标是 21,最小值是 16,我还更新了 sdk 和 adt ...

谁能帮助在棒棒糖之前的设备上正常工作?

【问题讨论】:

  • 答案对你有用吗?

标签: android toolbar android-5.0-lollipop


【解决方案1】:

请改用Theme.AppCompat.Light.NoActionBar

要呼叫setSupportActionBar,您不能有另一个操作栏。这就是你得到的原因

这个 Activity 已经有一个由窗口装饰提供的操作栏

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

【讨论】:

  • 工作!谢谢大家 ;)
猜你喜欢
  • 1970-01-01
  • 2019-11-18
  • 1970-01-01
  • 2016-07-04
  • 1970-01-01
  • 2016-05-02
  • 2015-06-27
  • 2016-11-23
  • 2015-02-07
相关资源
最近更新 更多