如何制作带有点的自定义textview?

时间:2018-11-19 18:04:35

标签: android xamarin.android drawing custom-view

我正在尝试制作日历,我想向用户显示有关在特定日期存储什么事件的某种信息。我当时正在考虑使用带有小点的颜色为其活动选择颜色,但我似乎并没有找到办法,即使有可能... 我有这个:calendar without dots 我想要这样的东西:enter image description here

有人可以帮助我,或给我一些提示吗?

1 个答案:

答案 0 :(得分:1)

您可以尝试使用第三方库material-calendarview来实现此功能,它提供了DayViewDecorators类来自定义个别日子:

  • 设置自定义背景
  • 设置自定义选择器
  • 适用于整天的文字
    • 我们提供DotSpan,它将在文本下方居中绘制一个点
  • 将日期设置为禁用

由于您正在使用Xamarin.Android,因此可以通过Binding a Java LibraryXamarin.Android项目中使用此Java库。

顺便说一下,其他人为您完成了这项工作:https://github.com/NAXAM/materialcalendarview-xamarin-android-binding,您可以直接将其安装在nuget package中。

使用量

XAML中使用它:

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.prolificinteractive.materialcalendarview.MaterialCalendarView
        android:id="@+id/calendar_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:background="@drawable/bg_custom_date_picker"
        app:mcv_selectionColor="@color/colorAccent"
        app:mcv_showOtherDates="other_months"
        android:layout_marginBottom="16dp"/>
</LinearLayout>

在您的代码中:

public class CalendarActivity : AppCompatActivity
{
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_calendar);

        MaterialCalendarView calendar = FindViewById<MaterialCalendarView>(Resource.Id.calendar_view);
        calendar.SetSelectedDate(Java.Util.Calendar.GetInstance(Java.Util.Locale.English));

        List<CalendarDay> dates = new List<CalendarDay>();

        CalendarDay a = CalendarDay.From(2018, 11, 10);
        CalendarDay b = CalendarDay.From(2018, 11, 15);
        CalendarDay c = CalendarDay.From(2018, 11, 9);
        CalendarDay d = CalendarDay.From(2018, 11, 1);

        dates.Add(a);
        dates.Add(b);
        dates.Add(c);
        dates.Add(d);

        calendar.AddDecorators(new EventDecorator(this, Color.Red, dates));
    }

    class EventDecorator : Java.Lang.Object, IDayViewDecorator
    {
        private int color;
        private List<CalendarDay> dates;

        public EventDecorator(CalendarActivity calendarActivity, int color, List<CalendarDay> dates)
        {
            this.color = color;
            this.dates = dates;
        }

        public void Decorate(DayViewFacade view)
        {
            view.AddSpan(new DotSpan(15, color));
        }

        public bool ShouldDecorate(CalendarDay day)
        {  
            //return dates.Contains(day);   
            //Has issue when using the above code: https://stackoverflow.com/questions/45193268/adding-decorations-using-materialcalendarview-binding-library-in-xamarin-android/45207338#45207338

            if (dates[0].ToString() == day.ToString() || dates[1].ToString() == day.ToString() || dates[2].ToString() == day.ToString() || dates[3].ToString() == day.ToString())
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}

Effect