此日历采用的是第三方库实现的:需要将mylibrary库导进来
布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <RelativeLayout android:padding="5dp" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:background="#CD3636"> <ImageView android:id="@+id/back" android:layout_width="50dp" android:layout_height="50dp" android:src="@drawable/back"/> <TextView android:id="@+id/title" android:layout_centerVertical="true" android:layout_centerHorizontal="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="2017/4/4" android:textSize="20dp" android:textColor="#F8E6E6"/> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView style="@style/date_style" android:text="日" /> <TextView style="@style/date_style" android:text="一" /> <TextView style="@style/date_style" android:text="二" /> <TextView style="@style/date_style" android:text="三" /> <TextView style="@style/date_style" android:text="四" /> <TextView style="@style/date_style" android:text="五" /> <TextView style="@style/date_style" android:text="六" /> </LinearLayout> <!--使用第三方的日历库--> <com.codbking.calendar.CalendarLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <com.codbking.calendar.CalendarDateView android:id="@+id/mCalendarDateView" android:layout_width="match_parent" android:layout_height="wrap_content"/> <ListView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="wrap_content"/> </com.codbking.calendar.CalendarLayout> </LinearLayout>
子布局文件:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="@drawable/calendar_click_bg_color"> <TextView android:id="@+id/text" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="4" /> <TextView android:id="@+id/chinatext" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="初八" /> </LinearLayout>
圆角代码:当点击日期的时候会有圆形的背景:
<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_selected="true"> <shape android:shape="oval"> <solid android:color="#cd3636"/> </shape> </item> <item android:drawable="@android:color/transparent"/> </selector>
CalendarActivity:
/** * 使用第三方的日历库。即mylibrary */ public class CalendarActivity extends AppCompatActivity { @Bind(R.id.back) ImageView back; @Bind(R.id.title) TextView title; @Bind(R.id.mCalendarDateView) CalendarDateView mCalendarDateView; @Bind(R.id.list) ListView list; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_calendar); ButterKnife.bind(this); initView(); } private void initView() { //用于显示日历的日期和农历的日期 mCalendarDateView.setAdapter(new CaledarAdapter() { @Override public View getView(View convertView, ViewGroup parentView, CalendarBean bean) { if(convertView==null){ convertView= LayoutInflater.from(CalendarActivity.this).inflate(R.layout.activity_calendar_item,null); } TextView text= (TextView) convertView.findViewById(R.id.text); TextView chinatext= (TextView) convertView.findViewById(R.id.chinatext); text.setText(""+bean.day);//显示阳历日期 //判断月份文本的颜色 if(bean.mothFlag!=0){ text.setTextColor(0xff9299a1); }else chinatext.setText(bean.chinaDay);//显示农历日期 return convertView; } }); //点击日历的监听事件 mCalendarDateView.setOnItemClickListener(new CalendarView.OnItemClickListener() { @Override public void onItemClick(View view, int postion, CalendarBean bean) { //当点击某一个日期,标题就会显示对应的日期 title.setText(bean.year+"/"+bean.moth+"/"+bean.day); } }); int[] date= CalendarUtil.getYMD(new Date()); title.setText(date[0]+"/"+date[1]+"/"+date[2]); } @OnClick(R.id.back) void back(){ // Intent intent=new Intent(CalendarActivity.this, MainActivity.class); // startActivity(intent); finish(); } }