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你可以找到样本这样做