android自訂日曆的使用方法

來源:互聯網
上載者:User

標籤:android   日曆   

子夜枯燈123提供了一個自訂的android日曆,他的部落格地址如下:

http://blog.csdn.net/huangyanbin123/article/details/38350213

他提供的代碼除了自訂日曆外,還有一個demo,對於大部分人來說,僅僅需要展示一個日曆,並響應日曆的click事件即可。因此我簡化了他的demo,給出了一個更加簡單的demo和使用方法,方法分為三步:

第一步:匯入

http://download.csdn.net/detail/huangyanbin123/7723323

中的代碼。因為這不是一個library項目,因此直接將原始碼加入你的工程即可。

第二步:建立一個如下的layout檔案,主要是android.support.v4.view.ViewPager要放在合適的位置,它是日曆控制項的容器。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:textcircle="http://schemas.android.com/apk/res/com.example.calendar"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="#fffffe"        android:orientation="vertical"        android:paddingLeft="10dip"        android:paddingRight="10dip" >        <View            android:layout_width="fill_parent"            android:layout_height="1px"            android:layout_alignParentTop="true"            android:background="#20000000" >        </View>        <TableLayout            android:layout_width="fill_parent"            android:layout_height="20dip"            android:layout_marginBottom="2dip"            android:layout_marginTop="2dip" >            <TableRow>                <TextView                    style="@style/dateStyle"                    android:text="日"                    android:textColor="@color/date_1" />                <TextView                    style="@style/dateStyle"                    android:text="一"                    android:textColor="@color/date_2" />                <TextView                    style="@style/dateStyle"                    android:text="二"                    android:textColor="@color/date_2" />                <TextView                    style="@style/dateStyle"                    android:text="三"                    android:textColor="@color/date_2" />                <TextView                    style="@style/dateStyle"                    android:text="四"                    android:textColor="@color/date_2" />                <TextView                    style="@style/dateStyle"                    android:text="五"                    android:textColor="@color/date_2" />                <TextView                    style="@style/dateStyle"                    android:text="六"                    android:textColor="@color/date_1" />            </TableRow>        </TableLayout>        <View            android:layout_width="fill_parent"            android:layout_height="1px"            android:layout_alignParentTop="true"            android:background="#20000000" >        </View>    </LinearLayout>    <RelativeLayout        android:id="@+id/contentPager2"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_weight="1"        android:background="#fffffe" >        <android.support.v4.view.ViewPager            android:id="@+id/viewpager2"            android:layout_width="fill_parent"            android:layout_height="wrap_content"            android:layout_gravity="center"            android:background="#F7F7F7"            android:paddingLeft="15dip"            android:paddingRight="10dip" >        </android.support.v4.view.ViewPager>    </RelativeLayout></LinearLayout>

第三步:建立自己的Activity類,如下所示:

package com.kidapk.www;import android.app.Activity;import android.os.Bundle;import android.support.v4.view.ViewPager;import android.widget.Toast;import com.example.calendar.R;import com.example.calendar.doim.CalendarViewBuilder;import com.example.calendar.doim.CustomDate;import com.example.calendar.widget.CalendarView;import com.example.calendar.widget.CalendarView.CallBack;import com.example.calendar.widget.CalendarViewPagerLisenter;import com.example.calendar.widget.CustomViewPagerAdapter;public class TestCalendarActivity extends Activity implements CallBack{private ViewPager viewPager;private CalendarView[] views;private CalendarViewBuilder builder = new CalendarViewBuilder();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_test);//初始化控制項viewPager = (ViewPager) this.findViewById(R.id.viewpager2);views = builder.createMassCalendarViews(this, 5, this);   //產生多個CalendarViewthis.findViewById(R.id.contentPager2);//設定控制項CustomViewPagerAdapter<CalendarView> viewPagerAdapter = new CustomViewPagerAdapter<CalendarView>(views);viewPager.setAdapter(viewPagerAdapter);viewPager.setCurrentItem(498);   //views可翻動498頁viewPager.setOnPageChangeListener(new CalendarViewPagerLisenter(viewPagerAdapter));}@Overridepublic void clickDate(CustomDate date) {Toast.makeText(this, date.year+"-"+date.month+"-"+date.day, Toast.LENGTH_SHORT).show();}@Overridepublic void onMesureCellHeight(int cellSpace) {}@Overridepublic void changeDate(CustomDate date) {}}
app的效果如所示:




android自訂日曆的使用方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.