【问题标题】:Android Toolbar not working [duplicate]Android工具栏不起作用[重复]
【发布时间】:2015-09-07 05:56:45
【问题描述】:

我正在尝试在 Android 中制作一个向后兼容的工具栏,并且我遵循了多个样式指南中给出的所有建议来尝试实现这一目标。但是,它似乎仍然不起作用。风格如下:

<resources>

    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primary_dark</item>
        <item name="android:windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
    </style>

    <color name="primary">#da291c</color>
    <color name="primary_dark">#9d1e14</color>
    <color name="white">#ffffff</color>

</resources>

这里是工具栏:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/primary" />

这里是主要的活动 onCreate 方法:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            setSupportActionBar(toolbar);
        }
    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
}

不太确定出了什么问题,因为我正在遵循许多来源给出的所有说明。这是日志:

java.lang.IllegalArgumentException: AppCompat does not support the current theme features

我一直在查看这段代码以及无数教程和 StackOverflow 问题,几个小时,但无济于事。如果有人可以帮助我,将不胜感激。在此先感谢:)

编辑 1: 这是我的清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.android.hackdaytest" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

【问题讨论】:

  • 你也可以粘贴你的清单文件
  • 当然,我刚刚做到了。

标签: android android-actionbar material-design android-toolbar


【解决方案1】:

将您的样式更改为:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

或继承Theme.AppCompat.Light.NoActionBar

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
</style>

参考:Chris Banes 的帖子here

【讨论】:

  • 谢谢!这似乎奏效了——部分有效。应用程序在没有运行时错误的情况下启动,但工具栏本身仍未显示。 MainActivity.java中是否有问题阻止ActionBar与工具栏切换?
  • 你有&lt;include/&gt;d 吗?你能发布你的布局xml吗?
  • 很抱歉,但我不确定这意味着什么。你能解释一下吗?
  • 您可以发布您的activity_main.xml 布局吗?我想弄清楚的是如何在布局中包含工具栏。
  • 哦,哇,没关系。我在不同的布局文件中有工具栏,所以 findViewById 返回 null。非常感谢您对原始问题的帮助!! :)
猜你喜欢
  • 2014-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-17
  • 2016-11-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多