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

时间:2015-10-26 15:57:37

标签: android android-layout android-activity android-studio android-actionbar

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

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

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

9 个答案:

答案 0 :(得分:11)

使用此代码

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

答案 1 :(得分: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()方法访问新的操作栏。告诉我它是否有帮助:)

答案 2 :(得分:1)

尝试这样:

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

答案 3 :(得分:1)

首先, ActionBar 被删除,使用工具栏(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)

答案 4 :(得分: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://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)

答案 5 :(得分:1)

这是Kotlin的代码

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

答案 6 :(得分: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);

实际信用额度为this link的SO。

答案 7 :(得分:0)

如果考虑到API 23 +。

,您有自定义工具栏

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

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

答案 8 :(得分:0)

这可能不是“最佳方法”,但我只是尝试了一下,并且效果很好,即使在有人找到更好的方法之前,它也应视为一种临时措施,并且可以使用PREAPI23 ... 您可以为工具栏的每次单独使用创建新的xml布局,例如,toolbar1.xml,toolbar2.xml等。然后在活动代码中将其添加到onCreate:

对于工具栏1:

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

对于工具栏2:

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

等...

希望这对某人有帮助!