Seb*_*hec 3 android material-design android-toolbar android-appbarlayout
如何设置的图标(家庭和溢出菜单图标)的颜色在Toolbar/ AppBarLayout 编程?
我想更改活动中单个片段的工具栏颜色方案.将AppBarLayout背景设置为浅色(例如浅灰色appBarLayout.setBackgroundResource(..);)会产生白色图标和白色标题,几乎看不到.
其布局如下:
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ToolbarStyle"
app:layout_scrollFlags="scroll|enterAlways"/>
</android.support.design.widget.AppBarLayout>
Run Code Online (Sandbox Code Playgroud)
支持23更改溢出图标很容易.这是Lorne Laliberte回答的方法
public static void setOverflowButtonColor(final Toolbar toolbar, final int color) {
Drawable drawable = toolbar.getOverflowIcon();
if(drawable != null) {
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable.mutate(), color);
toolbar.setOverflowIcon(drawable);
}
}
Run Code Online (Sandbox Code Playgroud)
你可以改变你的家庭,通过你的自定义drawable ..
getSupportActionBar().setHomeAsUpIndicator(R.drawable.your_drawable)
Run Code Online (Sandbox Code Playgroud)
或改变它的颜色
final Drawable upArrow = getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
upArrow.setColorFilter(Color.parseColor("#FFFFFF"), PorterDuff.Mode.SRC_ATOP);
getSupportActionBar().setHomeAsUpIndicator(upArrow);
Run Code Online (Sandbox Code Playgroud)
编辑: 如果你想更改更多的元素这里是改变所有工具栏图标颜色的好帖子.
希望这可以帮助!!
我接受了最有帮助的答案(并对此进行了评论),解释了我使用了其链接的代码片段的组合来形成单一AppBarLayout/ Toolbar着色方法。它涵盖了背景,标题,字幕,后退/抽屉图标和溢出图标的颜色,以及ImageButtons添加的任何自定义。这是我的结果(请原谅英语的“ colour”拼写(!)..):
public static void colouriseToolbar(AppBarLayout appBarLayout, @ColorInt int background, @ColorInt int foreground) {
if (appBarLayout == null) return;
appBarLayout.setBackgroundColor(background);
final Toolbar toolbar = (Toolbar)appBarLayout.getChildAt(0);
if (toolbar == null) return;
toolbar.setTitleTextColor(foreground);
toolbar.setSubtitleTextColor(foreground);
final PorterDuffColorFilter colorFilter
= new PorterDuffColorFilter(foreground, PorterDuff.Mode.MULTIPLY);
for (int i = 0; i < toolbar.getChildCount(); i++) {
final View view = toolbar.getChildAt(i);
//todo: cal icon?
Log.d(Globals.TAG, "view: "+i+" "+view.getClass().toString());
//Back button or drawer open button
if (view instanceof ImageButton) {
((ImageButton)view).getDrawable().setColorFilter(colorFilter);
}
if (view instanceof ActionMenuView) {
for (int j = 0; j < ((ActionMenuView) view).getChildCount(); j++) {
final View innerView = ((ActionMenuView)view).getChildAt(j);
//Any ActionMenuViews - icons that are not back button, text or overflow menu
if (innerView instanceof ActionMenuItemView) {
Log.d(Globals.TAG, "view (actionmenuitemviwe): "+i);
final Drawable[] drawables = ((ActionMenuItemView)innerView).getCompoundDrawables();
for (int k = 0; k < drawables.length; k++) {
final Drawable drawable = drawables[k];
if (drawable != null) {
final int drawableIndex = k;
//Set the color filter in separate thread
//by adding it to the message queue - won't work otherwise
innerView.post(new Runnable() {
@Override
public void run() {
((ActionMenuItemView) innerView).getCompoundDrawables()[drawableIndex].setColorFilter(colorFilter);
}
});
}
}
}
}
}
}
//Overflow icon
Drawable overflowIcon = toolbar.getOverflowIcon();
if (overflowIcon != null) {
overflowIcon.setColorFilter(colorFilter);
toolbar.setOverflowIcon(overflowIcon);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9087 次 |
| 最近记录: |