我在colors.xml
文件中有一个颜色,我需要将其用于toolbar
的颜色。
<resources>
<color name="MAIN_A">#f16264</color>
</resources>
现在我需要将MAIN_A
用作toolbar
的颜色。
我在colors.xml
文件中有一个颜色,我需要将其用于toolbar
的颜色。
<resources>
<color name="MAIN_A">#f16264</color>
</resources>
现在我需要将MAIN_A
用作toolbar
的颜色。
使用此代码
getSupportActionBar().setBackground(new ColorDrawable(getResources().getColor(R.color.white)));
supportActionBar!!.setBackgroundDrawable(ColorDrawable(resources.getColor(R.color.colorPrimary)))
<?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" />
toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
// set toolbar object as actionbar
setSupportActionBar(toolbar);
}
之后,您可以通过getSupportActionBar()方法访问您的新操作栏。如果有帮助,请告诉我 :)
尝试像这样:
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.MAIN_A)));
首先,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)
工具栏的背景、文本、箭头和三个点弹出菜单的颜色。
1)背景:
toolbar.setBackgroundColor(ContextCompat.getColor(this, R.color.toolbar_color));
toolbar.setBackground(new ColorDrawable(ContextCompat.getColor(this, R.color.toolbar_color)));
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://dev59.com/AV8d5IYBdhLWcg3weiF-#26837072和https://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)
Toolbar mToolbar = (Toolbar)findViewById(R.id.yourtoolbarId);
2 - mToolbar.setBackgroundColor(Color.parseColor("#004D40"));
从你的问题来看,你没有使用 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。
这可能不是“最佳方法”,但我刚刚尝试了一下,它可以正常工作,即使应该被视为临时措施,直到有人找到更好的方法,并且它适用于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);
等等……
希望这能帮到某些人!
ActionBar
本身已经被弃用了,在这里使用了getSupportActionBar()
。 - CoolMind