效果图在最底下 一、java类: public class Calendar extends AppCompatActivity { private CalendarView calendarView; private int[] cDate = CalendarUtil.getCurrentDate(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_calendar); final TextView title = (TextView) findViewById(R.id.title); //当前选中的日期 calendarView = (CalendarView) findViewById(R.id.calendar); View calendarContent = findViewById(R.id.calendar); // 获取 CalendarView if (calendarContent instanceof ViewGroup) { ViewGroup viewGroup = (ViewGroup) calendarContent; for (int i = 0; i < viewGroup.getChildCount(); i++) { View child = viewGroup.getChildAt(i); if (child instanceof TextView) { ((TextView) child).setTextSize(TypedValue.COMPLEX_UNIT_SP, 30); } } } calendarView .setStartEndDate("1949.1", "2050.12") .setDisableStartEndDate("1949.10.10", "2050.10.10") .setInitDate(cDate[0] + "." + cDate[1]) .setSingleDate(cDate[0] + "." + cDate[1] + "." + cDate[2]) .init(); title.setText(cDate[0] + "年" + cDate[1] + "月"); calendarView.setOnPagerChangeListener(new OnPagerChangeListener() { @Override public void onPagerChanged(int[] date) { title.setText(date[0] + "年" + date[1] + "月"); } }); } public void lastMonth(View view) { calendarView.lastMonth(); } // 上一月 public void nextMonth(View view) { calendarView.nextMonth(); } // 下一月 }
二、xml部分
<RelativeLayout 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:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".function.calendar.Calendar"> <LinearLayout android:id="@+id/leftLayout" android:layout_width="500dp" android:layout_height="match_parent" android:orientation="vertical" android:background="#729EF4" android:padding="20dp" android:layout_alignParentStart="true" android:layout_alignParentLeft="true"> <LinearLayout xmlns:calendarview="http://schemas.android.com/apk/res-auto" android:layout_width="800dp" android:layout_height="match_parent" android:background="@color/white" android:layout_marginLeft="500dp" android:orientation="vertical"> <RelativeLayout android:layout_width="800dp" android:layout_height="wrap_content" android:layout_marginTop="30dp"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:background="@mipmap/last" android:layout_marginLeft="20dp" android:onClick="lastMonth" /> <TextView android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:textSize="30sp" android:textStyle="bold" /> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentRight="true" android:layout_marginRight="50dp" android:background="@mipmap/next" android:onClick="nextMonth" /> </RelativeLayout> <com.othershe.calendarview.weiget.WeekView android:layout_width="800dp" android:layout_height="35dp" android:layout_marginTop="30dp" android:scrollbarSize="20dp"/> <com.othershe.calendarview.weiget.CalendarView android:id="@+id/calendar" android:layout_width="700dp" android:layout_height="match_parent" calendarview:choose_type="single" calendarview:show_holiday="true" calendarview:show_lunar="true" calendarview:show_term="true" android:layout_marginTop="25dp" calendarview:switch_choose="false" android:layout_marginLeft="35dp" calendarview:lunar_size="15" calendarview:solar_size="30"/> </LinearLayout> </RelativeLayout>
三、主要依赖部分
implementation 'com.github.Othershe:CalendarView:calendarview' implementation 'cn.6tail:lunar:sun' implementation 'com.github.Othershe:CalendarView:calendarview'标签:calendarView,CalendarView,安卓,日历,节气,cDate,title,void,TextView From: https://blog.csdn.net/qq_65679572/article/details/142753144