【问题标题】:ColorDrawable is always purple when displayed in Caldroid calendar cell在 Caldroid 日历单元格中显示时,ColorDrawable 始终为紫色
【发布时间】:2018-01-20 21:23:30
【问题描述】:

我正在为一个应用制作日历。我正在使用 Caldroid。在某些日期,我想更改背景颜色。如果我想用不同颜色着色的日期也是当前日期,那么我想要单元格上的红色边框以及该颜色。但是,我尝试显示的所有可绘制对象总是变成紫色。为什么是这样?我显示可绘制对象的逻辑如下所示:

ColorDrawable black = new ColorDrawable(R.drawable.black);
        ColorDrawable green = new ColorDrawable(R.drawable.green);
        ColorDrawable yellow = new ColorDrawable(R.drawable.yellow);
        ColorDrawable blue = new ColorDrawable(R.drawable.blue);

        ColorDrawable blackBordered = new ColorDrawable(R.drawable.red_border_for_black);
        ColorDrawable greenBordered = new ColorDrawable(R.drawable.red_border_for_green);
        ColorDrawable yellowBordered = new ColorDrawable(R.drawable.red_border_for_yellow);
        ColorDrawable blueBordered = new ColorDrawable(R.drawable.red_border_for_blue);

        if(differenceInDatesGreen < differenceInDatesBlack) {
            if (datesEqual(today.toString(), trashDay.toString())) {
                Log.d("Where in caldroid fragment setter", "greenBordered");
                caldroidFragment.setBackgroundDrawableForDate(greenBordered, trashDay);
            }
            else {
                Log.d("Where in caldroid fragment setter", "green");
                caldroidFragment.setBackgroundDrawableForDate(green, trashDay);
            }
        } else {
            if (datesEqual(today.toString(), trashDay.toString())) {
                Log.d("Where in caldroid fragment setter", "blackBordered");
                caldroidFragment.setBackgroundDrawableForDate(blackBordered, trashDay);
            }
            else {
                Log.d("Where in caldroid fragment setter", "black");
                caldroidFragment.setBackgroundDrawableForDate(black, trashDay);
            }
        }

        if (datesEqual(today.toString(), neighborhoodEvent.toString())) {
            Log.d("Where in caldroid fragment setter", "yellowBordered");
            caldroidFragment.setBackgroundDrawableForDate(yellowBordered, neighborhoodEvent);
        }
        else {
            Log.d("Where in caldroid fragment setter", "yellow");
            caldroidFragment.setBackgroundDrawableForDate(yellow, neighborhoodEvent);
        }

        if (datesEqual(today.toString(), neighborhoodEvent.toString())) {
            Log.d("Where in caldroid fragment setter", "blueBordered");
            caldroidFragment.setBackgroundDrawableForDate(blueBordered, specialEvent);
        } else {
            Log.d("Where in caldroid fragment setter", "blue");
            caldroidFragment.setBackgroundDrawableForDate(blue, specialEvent);
        }

        caldroidFragment.setTextColorForDate(R.color.White, trashDay);
        caldroidFragment.setTextColorForDate(R.color.White, neighborhoodEvent);
        caldroidFragment.setTextColorForDate(R.color.White, specialEvent);

我所有的可绘制 XML 代码都完全相同,除了颜色不同。 没有红色边框的黑色drawable

<?xml version="1.0" encoding="utf-8"?>

<item
    android:bottom="0dp"
    android:left="0dp"
    android:right="0dp"
    android:top="0dp">
    <shape android:shape="rectangle" >
        <stroke
            android:width="0dp"
            android:color="@color/Black" />

        <solid android:color="@color/Black" />
    </shape>
</item>

带有红色边框的黑色可绘制对象

<?xml version="1.0" encoding="utf-8"?>

<item
    android:bottom="0dp"
    android:left="0dp"
    android:right="0dp"
    android:top="0dp">
    <shape android:shape="rectangle" >
        <stroke
            android:width="2dp"
            android:color="@color/Red_For_Border" />

        <solid android:color="@color/Black" />
    </shape>
</item>

感谢所有帮助!!!

【问题讨论】:

  • 嗨@OwenScott,欢迎来到stackoverflow.com。可以举一个导致颜色错误的更小例子吗? (注意我不熟悉 Caldroid,所以如果这是最小的,那就让它保持不变。)

标签: java android xml android-drawable colordrawable


【解决方案1】:

ColorDrawable(int) 构造函数需要一个颜色值;您正在向它传递一个资源ID

正确的用法是以下之一:

new ColorDrawable(ContextCompat.getColor(this, R.color.my_color));

new ColorDrawable(Color.RED);

new ColorDrawable(0xffff0000);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-17
    • 1970-01-01
    相关资源
    最近更新 更多