我的colors.xml
文件中有一种颜色,我需要用于toolbar
颜色
<resources>
<color name="MAIN_A">#f16264</color>
</resources>
现在我需要使用MAIN_A
作为toolbar
的颜色。
使用此代码
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.white)));
尝试创建新的布局资源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()方法访问新的操作栏。告诉我它是否有帮助:)
试试这样:
ActionBar actionBar = getActionBar();
actionBar.setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.MAIN_A)));
首先,不推荐使用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)背景:
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/2914140和https://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)
这是Kotlin的代码
supportActionBar!!.setBackgroundDrawable(ColorDrawable(resources.getColor(R.color.colorPrimary)))
根据您的问题,您没有使用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。
如果你有一个考虑API 23+的自定义工具栏。
1 - Toolbar mToolbar = (Toolbar)findViewById(R.id.yourtoolbarId);
2 - mToolbar.setBackgroundColor(Color.parseColor("#004D40"));
这可能不是“最好的方法”,但我只是尝试了这个并且它工作正常,即使它应该被认为是一个临时措施,直到有人找到一个更好的,它工作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);
等等...
希望这有助于某人!