繁体   English   中英

如何使用调色板库设置操作栏的颜色?

[英]How do I set the color of the action bar using palette library?

我有一个 bitmap 用于在 imageview 中显示来自 firebase 的图像。 我希望状态栏和操作栏的 colors 都可以通过数据库中图片的鲜艳颜色进行调整。 我搜索了如何做到这一点,并在下面有以下代码

      Palette.from(myBitmap).generate(new Palette.PaletteAsyncListener() {
        @Override
        public void onGenerated(Palette palette) {

              android.app.ActionBar bar=setBackgrounddrawable(palette.getVibrantColor(getResources().getColor(R.color.colorPrimary)));
             getWindow().setStatusBarColor(palette.getVibrantColor(getResources().getColor(R.colorPrimary)));
        }
    });

但是我在获取操作栏的代码行上出现错误,说“设置背景可绘制不能应用于 int”当我删除用于设置操作栏颜色的代码行时,只设置了状态栏颜色而不是操作栏,我怎样才能设置动作栏的颜色呢?

如您所见, setBackgroundDrawable()收到一个参数Drawable是什么类型,

但是你传入一个Int Color值,然后你得到了错误。


我不测试它,但你可以尝试一下:

setBackgroundDrawable(new ColorDrawable(palette.getVibrantColor(getResources().getColor(R.color.colorPrimary))));

也许,它会很容易使用工具栏?

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        layout="@layout/app_bar_main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <androidx.viewpager.widget.ViewPager
        android:id="@+id/mainPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:layout_margin="4dp"
        />

</LinearLayout>

app_bar_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activities.MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <ImageView
                    android:layout_width="150dp"
                    android:layout_height="match_parent"
                    android:src="@drawable/logo_vector_dark"
                    />

            </LinearLayout>

        </androidx.appcompat.widget.Toolbar>

    </com.google.android.material.appbar.AppBarLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Activity 的 onCreate 中的设置工具栏:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

换颜色:

Palette.from(myBitmap).generate(new Palette.PaletteAsyncListener() {
    @Override
    public void onGenerated(Palette palette) {      
        toolbar.setBackgroundColor(palette.getVibrantColor(getResources().getColor(R.color.colorAccent)));
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM