15

我在Android(3.0)應用程序,有模仿的谷歌日曆中發現的周曆佈局的要求工作:如何爲Android蜂窩應用程序創建每週日曆視圖?

Google Calendar Weekly Layout

這些事件將會被基礎上,通過谷歌日曆外部請求API(我已經有這部分工作)。使用API​​,我可以獲取本週的活動列表,每個活動都有一個開始和結束的日期時間。我希望使用這些數據在類似於上面的視圖中嚮應用程序的用戶顯示預定的事件。

這是我到目前爲止有:

My Prototype Calendar View

的XML如下所示:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="800dp" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Calendar Title" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

    <RelativeLayout 
     android:id="@+id/relativeLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 

     <LinearLayout 
      android:id="@+id/linearLayout1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true" 
      android:layout_alignParentRight="true" 
      android:layout_alignParentTop="true" > 

      <TextView 
       android:id="@+id/textView2" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="1" 
       android:text="" /> 

      <TextView 
       android:id="@+id/textView3" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:gravity="center" 
       android:text="Sunday" /> 

      <TextView 
       android:id="@+id/textView4" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:gravity="center" 
       android:text="Monday" /> 

      <TextView 
       android:id="@+id/textView5" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:gravity="center" 
       android:text="Tuesday" /> 

      <TextView 
       android:id="@+id/textView6" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:gravity="center" 
       android:text="Wednesday" /> 

      <TextView 
       android:id="@+id/textView7" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:gravity="center" 
       android:text="Thursday" /> 

      <TextView 
       android:id="@+id/textView8" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:gravity="center" 
       android:text="Friday" /> 

      <TextView 
       android:id="@+id/textView9" 
       android:layout_width="0dp" 
       android:layout_height="wrap_content" 
       android:layout_weight="2" 
       android:gravity="center" 
       android:text="Saturday" /> 
     </LinearLayout> 
    </RelativeLayout> 

    <ScrollView 
     android:id="@+id/scrollView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:padding="0dp" 
     android:scrollbars="none" >" 

     <RelativeLayout 
      android:id="@+id/relativeLayout242" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:padding="0dp" > 


      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="0dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="40dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="80dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="120dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="160dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="200dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="240dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="280dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="320dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="360dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="400dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="440dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="480dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="520dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="560dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="600dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="640dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="680dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="720dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="760dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="800dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="840dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="880dp"/> 
      <View android:background="#aaa" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="920dp"/> 

      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="20dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="60dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="100dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="140dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="180dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="220dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="260dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="300dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="340dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="380dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="420dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="460dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="500dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="540dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="580dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="620dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="660dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="700dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="740dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="780dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="820dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="860dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="900dp"/> 
      <View android:background="#777" android:layout_width = "fill_parent" android:layout_height="1dp" android:layout_marginTop="940dp"/> 

      <LinearLayout 
       android:id="@+id/linearLayout2" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:padding="0dp" > 

       <RelativeLayout 
        android:id="@+id/relativeLayout2" 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="1" 
        android:padding="0dp" > 

        <View android:background="#aaa" android:layout_width = "1dp" android:layout_height="fill_parent" android:layout_alignParentRight="true"/> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="0dp" 
         android:gravity="center" 
         android:text="12am" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="40dp" 
         android:gravity="center" 
         android:text="1am" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="80dp" 
         android:gravity="center" 
         android:text="2am" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="120dp" 
         android:gravity="center" 
         android:text="3am" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="160dp" 
         android:gravity="center" 
         android:text="4am" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="200dp" 
         android:gravity="center" 
         android:text="5am" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="240dp" 
         android:gravity="center" 
         android:text="6am" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="280dp" 
         android:gravity="center" 
         android:text="7am" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="320dp" 
         android:gravity="center" 
         android:text="8am" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="360dp" 
         android:gravity="center" 
         android:text="9am" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="400dp" 
         android:gravity="center" 
         android:text="10am" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="440dp" 
         android:gravity="center" 
         android:text="11am" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="480dp" 
         android:gravity="center" 
         android:text="12pm" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="520dp" 
         android:gravity="center" 
         android:text="1pm" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="560dp" 
         android:gravity="center" 
         android:text="2pm" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="600dp" 
         android:gravity="center" 
         android:text="3pm" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="640dp" 
         android:gravity="center" 
         android:text="4pm" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="680dp" 
         android:gravity="center" 
         android:text="5pm" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="720dp" 
         android:gravity="center" 
         android:text="6pm" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="760dp" 
         android:gravity="center" 
         android:text="7pm" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="800dp" 
         android:gravity="center" 
         android:text="8pm" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="840dp" 
         android:gravity="center" 
         android:text="9pm" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_marginTop="880dp" 
         android:gravity="center" 
         android:text="10pm" /> 

        <TextView 
         android:id="@+id/textView10" 
         android:layout_width="match_parent" 
         android:layout_height="40dp" 
         android:layout_marginTop="920dp" 
         android:gravity="center|top" 
         android:text="11pm" /> 
       </RelativeLayout> 

       <RelativeLayout 
        android:id="@+id/relativeLayout3" 
        android:layout_width="0dp" 
        android:layout_height="match_parent" 
        android:layout_weight="14" 
        android:padding="0dp" > 

        <LinearLayout 
         android:id="@+id/linearLayout3" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content" 
         android:layout_alignParentLeft="true" 
         android:layout_alignParentRight="true" 
         android:layout_alignParentTop="true" 
         android:padding="0dp" > 

         <RelativeLayout 
          android:id="@+id/relativeLayout4" 
          android:layout_width="0dp" 
          android:layout_height="match_parent" 
          android:layout_weight="1" > 

          <View android:background="#00f" android:layout_width = "fill_parent" android:layout_height="180dp" android:layout_marginTop="180dp"/> 

          <Button 
           android:id="@+id/button1" 
           android:layout_width="fill_parent" 
           android:layout_height="180dp" 
           android:layout_marginTop="180dp" 
           android:text="Some Event" /> 

         </RelativeLayout> 

         <RelativeLayout 
          android:id="@+id/relativeLayout5" 
          android:layout_width="0dp" 
          android:layout_height="match_parent" 
          android:layout_weight="1" > 

          <View android:background="#00f" android:layout_width = "fill_parent" android:layout_height="180dp" android:layout_marginTop="280dp"/> 

          <Button 
           android:id="@+id/button1" 
           android:layout_width="fill_parent" 
           android:layout_height="180dp" 
           android:layout_marginTop="280dp" 
           android:text="Some Event" /> 

         </RelativeLayout> 

         <RelativeLayout 
          android:id="@+id/relativeLayout6" 
          android:layout_width="0dp" 
          android:layout_height="match_parent" 
          android:layout_weight="1" > 

          <View android:background="#00f" android:layout_width = "fill_parent" android:layout_height="60dp" android:layout_marginTop="40dp"/> 

          <Button 
           android:id="@+id/button1" 
           android:layout_width="fill_parent" 
           android:layout_height="60dp" 
           android:layout_marginTop="40dp" 
           android:text="Some Event" /> 

         </RelativeLayout> 

         <RelativeLayout 
          android:id="@+id/relativeLayout7" 
          android:layout_width="0dp" 
          android:layout_height="match_parent" 
          android:layout_weight="1" > 

          <View android:background="#00f" android:layout_width = "fill_parent" android:layout_height="90dp" android:layout_marginTop="60dp"/> 

          <Button 
           android:id="@+id/button1" 
           android:layout_width="fill_parent" 
           android:layout_height="90dp" 
           android:layout_marginTop="60dp" 
           android:text="Some Event" /> 

          <View android:background="#00f" android:layout_width = "fill_parent" android:layout_height="120dp" android:layout_marginTop="340dp"/> 

          <Button 
           android:id="@+id/button1" 
           android:layout_width="fill_parent" 
           android:layout_height="120dp" 
           android:layout_marginTop="340dp" 
           android:text="Some Event" /> 

         </RelativeLayout> 

         <RelativeLayout 
          android:id="@+id/relativeLayout8" 
          android:layout_width="0dp" 
          android:layout_height="match_parent" 
          android:layout_weight="1" > 

          <View android:background="#00f" android:layout_width = "fill_parent" android:layout_height="180dp" android:layout_marginTop="380dp"/> 

          <Button 
           android:id="@+id/button1" 
           android:layout_width="fill_parent" 
           android:layout_height="180dp" 
           android:layout_marginTop="380dp" 
           android:text="Some Event" /> 

         </RelativeLayout> 

         <RelativeLayout 
          android:id="@+id/relativeLayout9" 
          android:layout_width="0dp" 
          android:layout_height="match_parent" 
          android:layout_weight="1" > 

          <View android:background="#00f" android:layout_width = "fill_parent" android:layout_height="180dp" android:layout_marginTop="480dp"/> 

          <Button 
           android:id="@+id/button1" 
           android:layout_width="fill_parent" 
           android:layout_height="180dp" 
           android:layout_marginTop="480dp" 
           android:text="Some Event" /> 

         </RelativeLayout> 

         <RelativeLayout 
          android:id="@+id/relativeLayout10" 
          android:layout_width="0dp" 
          android:layout_height="match_parent" 
          android:layout_weight="1" > 

          <View android:background="#00f" android:layout_width = "fill_parent" android:layout_height="180dp" android:layout_marginTop="340dp"/> 

          <Button 
           android:id="@+id/button1" 
           android:layout_width="fill_parent" 
           android:layout_height="180dp" 
           android:layout_marginTop="340dp" 
           android:text="Some Event" /> 

         </RelativeLayout>   

        </LinearLayout> 
       </RelativeLayout> 
      </LinearLayout> 
     </RelativeLayout> 
    </ScrollView> 

</LinearLayout> 

我的做法是使40dp等於時間1個小時。因此,每當我想添加一個持續時間爲1.5小時的事件時,我會製作一個60dp按鈕,我將把它放在時間開始的確切位置(上午12am = 0dp,下午1pm =頂部40dp ,2pm =從頂部80d等)。

我的問題是:

  1. 是否有這樣做的更好的辦法?
  2. 如何將我的XML轉換爲可以添加到任何Android項目的獨立視圖? (我計劃在可能發佈有關最終產品的博客文章)

謝謝!

+0

哎BVB如何[Rú???我需要像你一樣的日曆given.hv你做了這個觀點的代碼?我不明白鋤頭這樣做,所以如果你的代碼,然後你可以給我嗎?/ – Google 2012-03-20 06:21:10

+0

你是什麼意思?哪個視圖?第一個是Google日曆(Web應用程序)的屏幕截圖,第二個是發佈的XML結果的屏幕截圖。 – BVB 2012-03-21 07:14:49

+0

嘿bvb我想用第一個nd我不能做到這一點,使用谷歌api.if你的高保真代碼然後發送給我,所以我可以看到如何工作? – Google 2012-03-21 08:48:25

回答

18

我結束了以下類似的方法,我原來的職位。我在裏面創建了一個線性佈局的滾動視圖。然後,我添加了七個相對佈局線性佈局。每個相對佈局都是一天。我確信這些佈局的高度等於一天中的分鐘數。這會使1小時= 60分鐘= 60 dp,這使得測量事件的高度更容易。對於事件,我創建了一個自定義視圖,可以顯示事件的開始和結束時間以及事件的標題。使用layout_marginTop屬性將事件添加到相關佈局,其值等於事件的開始時間(從一天開始的分鐘數)。這似乎工作得很好。

下面是什麼它結束了看起來像一個預覽:

Calendar preview

這裏的XML佈局:http://pastebin.com/jT4wQxeb

代碼太長,以適應答案。

注:calendar_zebra只是一個60 * 24 = 1440dp的高佈局,1dp高水平View s,每60dp放置一個純灰色背景。每個代表一個小時的標記。

+0

我必須創建日曆視圖,與您創建的相同。我瀏覽了您提供的鏈接,但無法理解如何創建每週視圖。請你幫我在這... – Narendra 2013-07-09 12:08:53

+0

我已經發布了代碼。請看這裏的評論:http://aleakymemory.blogspot.com/2012/09/android-room-manager.html – BVB 2013-07-09 18:14:11

+0

請發佈Xml – 2014-03-24 07:21:43

3

1。 以下是我從Calendar

它可以爲你的XML整個佈局定製視圖(DayView在一天的議程)來源弄清楚。

然後它使用矩形和文本繪製畫布,就像調用onDraw()時應用程序中看到的一樣。

視圖實現了事件的OnClickListenerOnLongClickListener。當您點擊畫布時,它會將您的點擊位置轉移到具有相應日期和時間的事件(因此,當調用覆蓋onSizeChanged()時,它會計算大量事件,保證金,全天事件的相關大小),然後啓動活動if這樣的事件存在,否則創建一個新的事件。

2.You需要創建一個類繼承查看,說DayView,那麼你應該加入到這個attrs.xml

<resources> 
    <declare-styleable name="DayView"> 
    </declare-styleable> 
</resources> 

然後你可以使用它作爲你的包名在佈局的XML標記。喜歡這裏:

<xxxxxxx.DayView 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:id="@id/ui_schedule_dayview"> 

Here你可以找到樣本這樣做

相關問題