如何将活动中的工具栏背景颜色设置为colors.xml文件中的颜色?

10

我在colors.xml文件中有一个颜色,我需要将其用于toolbar的颜色。

<resources>
    <color name="MAIN_A">#f16264</color>
</resources>

现在我需要将MAIN_A用作toolbar的颜色。

9个回答

20

使用此代码

getSupportActionBar().setBackground(new ColorDrawable(getResources().getColor(R.color.white)));

setBackgroundDrawable已经被弃用。 - CoolMind
@CoolMind 不是这样的。在其他类中已经被弃用,但在 ActionBar.class 中没有。 - GabrielBB
@GabrielBB,嗯,也许你是对的,但是ActionBar本身已经被弃用了,在这里使用了getSupportActionBar() - CoolMind

3
这里是 Kotlin 的代码。
supportActionBar!!.setBackgroundDrawable(ColorDrawable(resources.getColor(R.color.colorPrimary)))

2
尝试创建新的布局资源toolbar.xml:
<?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/MAIN_A" />

然后像这样将其包括在您的活动布局中:

    <include
    android:id="@+id/toolbar"
    layout="@layout/toolbar" />

然后你需要在Activity的onCreate方法中设置这个工具栏:
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        // set toolbar object as actionbar
        setSupportActionBar(toolbar);
    }

之后,您可以通过getSupportActionBar()方法访问您的新操作栏。如果有帮助,请告诉我 :)


2

尝试像这样:

ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.MAIN_A)));

2

首先,ActionBar已经被弃用,应该使用Toolbar(android.widget.Toolbar)代替。 如果无法实现,请尝试如下方式使用ActionBar的支持:

android.support.v7.app.ActionBar actionBar = getSupportActionBar();

actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.MAIN_A)));

对于工具栏,它是:

toolbar.setBackgroundResource(R.color.MAIN_A)

如何更改工具栏的背景颜色? - user5148540
@user5148540 toolbar.setBackgroundResource(R.color.MAIN_A) - Hichem Acher

1

工具栏的背景文本箭头和三个点弹出菜单的颜色

1)背景:

toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.toolbar_color));

或者(需要API 16):
toolbar.setBackground(new ColorDrawable(ContextCompat.getColor(this, R.color.toolbar_color)));

2)标题:
toolbar.setTitleTextColor(ContextCompat.getColor(this, R.color.gray);

3)箭头:
toolbar.getNavigationIcon().setColorFilter(ContextCompat.getColor(this, R.color.gray), PorterDuff.Mode.SRC_ATOP);

4) 弹出菜单三个点的图标(右侧图标):

toolbar.getOverflowIcon().setColorFilter(ContextCompat.getColor(this, R.color.gray, PorterDuff.Mode.SRC_ATOP);

请看https://dev59.com/AV8d5IYBdhLWcg3weiF-#26837072https://dev59.com/wmgu5IYBdhLWcg3wTVU6#51908890

全部内容(使用Kotlin):

toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.blue))
val toolbarTextColor = ContextCompat.getColor(this, R.color.gray)
toolbar.setTitleTextColor(toolbarTextColor)
toolbar.navigationIcon?.setColorFilter(toolbarTextColor, PorterDuff.Mode.SRC_ATOP)
toolbar.overflowIcon?.setColorFilter(toolbarTextColor, PorterDuff.Mode.SRC_ATOP)

1
如果您有一个自定义的工具栏,考虑使用API 23+。
1 - Toolbar mToolbar = (Toolbar)findViewById(R.id.yourtoolbarId); 2 - mToolbar.setBackgroundColor(Color.parseColor("#004D40"));

0

从你的问题来看,你没有使用 SupportActionBar ,因此你可以像下面这样做:

ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.MAIN_A)));
actionBar.setDisplayShowTitleEnabled(false);  // required to force redraw, without, gray color
actionBar.setDisplayShowTitleEnabled(true);

实际的功劳归于 这个链接 中 Stack Overflow。


0

这可能不是“最佳方法”,但我刚刚尝试了一下,它可以正常工作,即使应该被视为临时措施,直到有人找到更好的方法,并且它适用于API23之前的版本...

您可以为每个单独使用工具栏的用途创建一个新的xml布局,例如toolbar1.xml、toolbar2.xml等。然后在活动代码中,在onCreate中添加以下内容:

对于toolbar1:

getSupportActionBar().setBackgroundDrawable(new colorDrawable(getResources().getColor(R.color.colorAdmin))); 

LayoutInflater inflater_admin = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

@SuppressLint("InflateParams") View action_bar_view_admin = Objects.requireNonNull(inflater_admin).inflate(R.layout.chat_custom_bar_admin, null);

actionBar.setCustomView(action_bar_view_admin);

针对toolbar2:

getSupportActionBar().setBackgroundDrawable(new colorDrawable(getResources().getColor(R.color.colorOwner))); 

LayoutInflater inflater_owner = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

@SuppressLint("InflateParams") View action_bar_view_owner = Objects.requireNonNull(inflater_owner).inflate(R.layout.chat_custom_bar_owner, null);

actionBar.setCustomView(action_bar_view_owner);

等等……

希望这能帮到某些人!


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