在Caldroid日历单元格中显示时,ColorDrawable始终为紫色

时间:2018-01-20 21:23:30

标签: java android xml android-drawable colordrawable

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

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代码都完全相同。 黑色的抽屉没有红色边框

<?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>

所有帮助表示赞赏!!!

1 个答案:

答案 0 :(得分:0)

ColorDrawable(int)构造函数需要颜色值;您传递的是资源ID

正确使用将是以下之一:

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

new ColorDrawable(Color.RED);

new ColorDrawable(0xffff0000);