Java Can';不要更改工具栏的颜色

Java Can';不要更改工具栏的颜色,java,android,android-actionbar,toolbar,Java,Android,Android Actionbar,Toolbar,任何人都可以解释一下,为什么我的工具栏没有改变颜色,即使我创建了另一种样式,如: <style name="AppTheme.NoActionBar"> <item name="windowActionBar">false</item> <item name="windowNoTitle">false</item> </style> 但仍然得到 此活动已具有由window decor提供的操作栏。 不要请求

任何人都可以解释一下,为什么我的工具栏没有改变颜色,即使我创建了另一种样式,如:

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">false</item>
</style>
但仍然得到

此活动已具有由window decor提供的操作栏。 不要请求Window.FEATURE\u ACTION\u栏并将windowActionBar设置为 如果主题为false,则改为使用工具栏

我甚至尝试
getSupportActionBar().hide()但仍然不起作用

XML:



基本上,我想要的是保留我的全局主题,并在其中一个活动中更改工具栏颜色。这就是为什么我不在xml文件中创建工具栏,而是以编程方式创建它,添加背景色并想对其进行设置。

您需要在
元数据
标记之外去掉
android:theme=“@style/AppTheme.NoActionBar”

  <activity
        android:name=".SettingsActivity"
        android:label="@string/title_activity_settings"
        android:theme="@style/AppTheme.NoActionBar"
        android:parentActivityName=".MainActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="net.noorr.menote.menote.MainActivity" />
    </activity>

我认为你的工具栏弄错了。您可能没有扩展
工具栏
类。 尝试通过xml文件中定义的id直接获取它

Toolbar toolbar = (Toolbar) findViewById(R.id.myToolbar);
然后您可以将其设置为支持,并最终设置颜色

编辑: 没有看到您正在以编程方式创建它。 尝试设置一个简单的AppTheme样式,如

<style name="NoToolbarStyle" parent="Theme.AppCompat.Light">
    <item name="android:windowActionBar">false</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>

假的
真的
假的
真的

希望下面的代码能帮助您

在styles.xml中创建主题,如下所示:

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>
在布局xml文件中添加此工具栏标记

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="#2196F3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
这可能对你有帮助

下面是
style.xml
包含自定义主题

<resources>

<style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>

<style name="AppTheme" parent="AppBaseTheme"></style>

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

布局
文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/settingRootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.demo.SettingActivity" >

</RelativeLayout>

我不想改变我的整个应用程序主题。我只想在一个活动中进行更改,但结果是出现了一个白色的空白屏幕。我用另一种方法解决了它,放弃了编程。但我的主要问题是,我的主题在元标记中,而你的回答让我找到了正确的方向。非常感谢。主题不在
中。它放在开口处
。标记。你能用你想要更改的工具栏元素发布你的xml文件吗?我想,你首先需要添加
工具栏
<代码>根元素。添加(工具栏)
。然后将其设置为
setSupportActionBar
@Ka7Im1011我添加了xml文件,请检查它。谢谢Summ,我想没有,因为我没有找到ViewById,我创建了新的工具栏非常感谢,但它不起作用。而ActionBarActivity是deprecated@Elnoor你说它不起作用是什么意思。它是否崩溃或工具栏未显示?@Elnoor您也可以使用AppCompatActivity来代替ActionBarActivity,还可以编辑ans。请参阅了解moe解释工具栏,但所有空白区域均显示白色事件通知区域。我想保留我的主题,只是改变工具栏的颜色。不管怎样,我使用AppCompatActivity,我提到它是因为它在你的代码中仍然不起作用@Ka7Im1011。我得到白色的通知区。
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat">
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>
compile "com.android.support:appcompat-v7:21.0.+"
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="#2196F3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
<activity
        android:name="com.javatechig.sample.MyActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;

public class MyActivity extends AppCompatActivity{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);

    // Set a toolbar to replace the action bar.
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}
}
<resources>

<style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>

<style name="AppTheme" parent="AppBaseTheme"></style>

<style name="CustomTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primary_dark</item>
    <item name="colorAccent">@color/accent</item>
</style>
<activity
    android:name=".SettingActivity"
    android:label="@string/title_activity_setting"
    android:theme="@style/CustomTheme" >
</activity>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/settingRootLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.demo.SettingActivity" >

</RelativeLayout>
package com.example.demo;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;

@SuppressLint("NewApi")
public class SettingActivity extends AppCompatActivity {

    private RelativeLayout rootLayout;
    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        rootLayout = (RelativeLayout) findViewById(R.id.settingRootLayout);
        mToolbar = new Toolbar(this);
        mToolbar.setLayoutParams(
                new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        mToolbar.setBackgroundColor(getResources().getColor(R.color.light_blue));
        mToolbar.setFitsSystemWindows(true);
        rootLayout.addView(mToolbar);
        setSupportActionBar(mToolbar);
    }

}