Android自定義日歷效果
因?yàn)楣ぷ鞴δ苄枨?,自定義一個(gè)日歷,效果如下,點(diǎn)擊選中日歷
使用github上面一個(gè)前輩的框架
implementation 'com.necer.ncalendar:ncalendar:5.0.0' implementation 'com.github.CodingEnding:PopupLayout:v1.0'//poplayout
框架使用基本類型地址,大家可以根據(jù)需要學(xué)習(xí)修改:地址
自定義日歷的xml文件
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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="wrap_content" tools:context=".CalendarActivity"> <View android:id="@+id/title_bar" android:layout_width="320dp" android:layout_height="40dp" android:background="@drawable/calendar_bg" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/year" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="18dp" android:text="2018" android:textColor="#ffffff" android:textSize="16sp" app:layout_constraintBottom_toBottomOf="@id/title_bar" app:layout_constraintStart_toStartOf="@id/title_bar" app:layout_constraintTop_toTopOf="@id/title_bar" /> <TextView android:id="@+id/month" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="18dp" android:text="四" android:textColor="#ffffff" android:textSize="18sp" app:layout_constraintBottom_toBottomOf="@id/title_bar" app:layout_constraintEnd_toEndOf="@id/title_bar" app:layout_constraintStart_toStartOf="@id/title_bar" app:layout_constraintTop_toTopOf="@id/title_bar" /> <TextView android:id="@+id/monthName" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="月" android:textColor="#ffffff" android:textSize="18sp" app:layout_constraintBottom_toBottomOf="@id/title_bar" app:layout_constraintStart_toEndOf="@id/month" app:layout_constraintTop_toTopOf="@id/title_bar" /> <com.necer.view.WeekBar android:id="@+id/week" android:layout_width="320dp" android:layout_height="wrap_content" android:layout_marginTop="10dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/title_bar" /> <com.necer.calendar.MonthCalendar android:id="@+id/month_calendar" android:layout_width="320dp" android:layout_height="280dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/week" /> <View android:id="@+id/bottom_view" android:layout_width="320dp" android:layout_height="40dp" android:background="@drawable/calendar_bg_bottom" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/month_calendar" /> <TextView android:id="@+id/lastYear" android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:onClick="lastYear" android:text="上一年" android:textColor="#ffffff" app:layout_constraintBottom_toBottomOf="@id/bottom_view" app:layout_constraintEnd_toStartOf="@id/dividerOne" app:layout_constraintStart_toStartOf="@id/bottom_view" app:layout_constraintTop_toTopOf="@id/bottom_view" /> <View android:id="@+id/dividerOne" android:layout_width="1dp" android:layout_height="40dp" android:background="#ffffff" app:layout_constraintEnd_toStartOf="@id/lastMonth" app:layout_constraintStart_toEndOf="@id/lastYear" app:layout_constraintTop_toTopOf="@id/bottom_view" /> <TextView android:id="@+id/lastMonth" android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:onClick="lastMonth" android:text="上個(gè)月" android:textColor="#ffffff" app:layout_constraintBottom_toBottomOf="@id/bottom_view" app:layout_constraintEnd_toStartOf="@id/dividerTwo" app:layout_constraintStart_toEndOf="@id/dividerOne" app:layout_constraintTop_toTopOf="@id/bottom_view" /> <View android:id="@+id/dividerTwo" android:layout_width="1dp" android:layout_height="40dp" android:background="#ffffff" app:layout_constraintEnd_toStartOf="@id/nextMonth" app:layout_constraintStart_toEndOf="@id/lastMonth" app:layout_constraintTop_toTopOf="@id/bottom_view" /> <TextView android:id="@+id/nextMonth" android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:text="下個(gè)月" android:textColor="#ffffff" android:onClick="nextMonth" app:layout_constraintBottom_toBottomOf="@id/bottom_view" app:layout_constraintEnd_toStartOf="@id/dividerThree" app:layout_constraintStart_toEndOf="@id/dividerTwo" app:layout_constraintTop_toTopOf="@id/bottom_view" /> <View android:id="@+id/dividerThree" android:layout_width="1dp" android:layout_height="40dp" android:background="#ffffff" app:layout_constraintEnd_toStartOf="@id/nextYear" app:layout_constraintStart_toEndOf="@id/nextMonth" app:layout_constraintTop_toTopOf="@id/bottom_view" /> <TextView android:id="@+id/nextYear" android:layout_width="0dp" android:layout_height="wrap_content" android:gravity="center" android:text="下一年" android:textColor="#ffffff" android:onClick="nextYear" app:layout_constraintBottom_toBottomOf="@id/bottom_view" app:layout_constraintEnd_toEndOf="@id/bottom_view" app:layout_constraintStart_toEndOf="@id/dividerThree" app:layout_constraintTop_toTopOf="@id/bottom_view" /> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity,日歷的功能重寫也是在和這個(gè)函數(shù)中
package com.example.calendartest; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.os.Handler; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.codingending.popuplayout.PopupLayout; import com.necer.calendar.BaseCalendar; import com.necer.calendar.MonthCalendar; import com.necer.enumeration.CheckModel; import com.necer.enumeration.DateChangeBehavior; import com.necer.listener.OnCalendarChangedListener; import org.joda.time.LocalDate; public class MainActivity extends AppCompatActivity { PopupLayout popupLayout; View calendarView; TextView mYear, mMonth, lastYear, nextYear, lastMonth, nextMonth; MonthCalendar monthCalendar; int currentYear, currentMonth; Button intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); intent = findViewById(R.id.intent); calendarView = View.inflate(this, R.layout.calendar, null); popupLayout = PopupLayout.init(this, calendarView); } public void intent(View view) { initCalendar(); popupLayout.show(PopupLayout.POSITION_CENTER); } public void initCalendar() { monthCalendar = calendarView.findViewById(R.id.month_calendar); mYear = calendarView.findViewById(R.id.year); mMonth = calendarView.findViewById(R.id.month); lastYear = calendarView.findViewById(R.id.lastYear); nextYear = calendarView.findViewById(R.id.nextYear); lastMonth = calendarView.findViewById(R.id.lastMonth); nextMonth = calendarView.findViewById(R.id.nextMonth); monthCalendar.setCheckMode(CheckModel.SINGLE_DEFAULT_UNCHECKED); monthCalendar.setOnCalendarChangedListener(new OnCalendarChangedListener() { @Override public void onCalendarChange(BaseCalendar baseCalendar, int year, int month, LocalDate localDate, DateChangeBehavior dateChangeBehavior) { mYear.setText(String.valueOf(year)); mMonth.setText(String.valueOf(month)); intent.setText(String.valueOf(localDate)); currentYear = year; currentMonth = month; new Handler().postDelayed(new Runnable() { @Override public void run() { popupLayout.dismiss(); } },800); } }); } public void lastMonth(View view) { monthCalendar.toLastPager(); } public void nextMonth(View view) { monthCalendar.toNextPager(); } public void nextYear(View view) { monthCalendar.jumpDate(currentYear + 1, currentMonth, 1); } public void lastYear(View view) { monthCalendar.jumpDate(currentYear - 1, currentMonth, 1); } }
GitHub下載地址
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)可滑動的自定義日歷控件
- Android實(shí)現(xiàn)帶簽到贏積分功能的日歷
- Android可簽到日歷控件的實(shí)現(xiàn)方法
- Android自定義日歷滑動控件
- 基于Android week view仿小米和iphone日歷效果
- Android自定義可標(biāo)記日歷效果
- Android自定義控件實(shí)現(xiàn)可多選課程日歷CalendarView
- Android 一個(gè)日歷控件的實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)自定義日歷
- android 開發(fā)教程之日歷項(xiàng)目實(shí)踐(一)
相關(guān)文章
Android自定義View制作動態(tài)炫酷按鈕實(shí)例解析
這篇文章主要為大家詳細(xì)解析了Android自定義View制作動態(tài)炫酷按鈕實(shí)例,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07Android利用Theme自定義Activity間的切換動畫
這篇文章主要為大家詳細(xì)介紹了Android利用Theme自定義Activity間的切換動畫,感興趣的小伙伴們可以參考一下2016-09-09Android?NDK入門初識(組件結(jié)構(gòu)開發(fā)流程)
這篇文章主要為大家介紹了Android?NDK入門之初識組件結(jié)構(gòu)開發(fā)流程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08Android7.0版本影響開發(fā)的改進(jìn)分析
這篇文章主要介紹了Android7.0版本影響開發(fā)的改進(jìn),總結(jié)分析了Android7.0版本中比較常見的開發(fā)注意事項(xiàng)與操作技巧,需要的朋友可以參考下2017-11-11Android ViewDragHelper實(shí)現(xiàn)京東、淘寶拖拽詳情功能的實(shí)現(xiàn)
這篇文章主要介紹了Android ViewDragHelper實(shí)現(xiàn)京東、淘寶拖拽詳情,實(shí)現(xiàn)這種效果大概分為三種方式,具體哪三種方式大家通過本文了解下吧2018-04-04XListView實(shí)現(xiàn)多條目網(wǎng)絡(luò)數(shù)據(jù)刷新加載 網(wǎng)絡(luò)加載圖片
這篇文章主要為大家詳細(xì)介紹了XListView實(shí)現(xiàn)多條目網(wǎng)絡(luò)數(shù)據(jù)刷新加載,網(wǎng)絡(luò)加載圖片,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11Android Vibrator調(diào)節(jié)震動代碼實(shí)例
這篇文章主要介紹了Android Vibrator調(diào)節(jié)震動代碼實(shí)例,本文直接給出實(shí)現(xiàn)代碼,代碼中包含詳細(xì)注釋,需要的朋友可以參考下2015-05-05詳解Android中visibility屬性VISIBLE、INVISIBLE、GONE的區(qū)別
在Android開發(fā)中,大部分控件都有visibility這個(gè)屬性,其屬性有3個(gè)分別為“visible ”、“invisible”、“gone”。主要用來設(shè)置控制控件的顯示和隱藏。本文就詳細(xì)的講解一下。2016-12-12