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

问题描述 投票:4回答:9

我的colors.xml文件中有一种颜色,我需要用于toolbar颜色

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

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

android android-layout android-activity android-studio android-actionbar
9个回答
12
投票

使用此代码

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

1
投票

尝试创建新的布局资源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" />

然后,您需要在活动onCreate方法中设置此工具栏:

    toolbar = (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        // set toolbar object as actionbar
        setSupportActionBar(toolbar);
    }

之后,您可以从getSupportActionBar()方法访问新的操作栏。告诉我它是否有帮助:)


1
投票

试试这样:

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

1
投票

首先,不推荐使用Action Bar,而是使用工具栏(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)

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);

箭头:

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://stackoverflow.com/a/26837072/2914140https://stackoverflow.com/a/51908890/2914140

一体化(在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
投票

这是Kotlin的代码

supportActionBar!!.setBackgroundDrawable(ColorDrawable(resources.getColor(R.color.colorPrimary)))

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);

实际信用额度为SO的this link


0
投票

如果你有一个考虑API 23+的自定义工具栏。

1 - Toolbar mToolbar = (Toolbar)findViewById(R.id.yourtoolbarId);

2 - mToolbar.setBackgroundColor(Color.parseColor("#004D40"));


0
投票

这可能不是“最好的方法”,但我只是尝试了这个并且它工作正常,即使它应该被认为是一个临时措施,直到有人找到一个更好的,它工作PRE-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);

等等...

希望这有助于某人!

© www.soinside.com 2019 - 2024. All rights reserved.