2012-04-25 69 views
1

我想在下图中创建一个类似日历的视图,但我不知道从哪里开始。我想找到最好的方法。我想到了GridView,但没有成功。有人有什么建议吗?设计日历视图

enter image description here

+0

我更新了我的答案,提供了一个可以让您的日历工作的概念。 – AedonEtLIRA 2012-04-25 15:06:45

回答

1

Android不提供SDK中的任何日历视图。从开发人员的角度来看,这是一个巨大的损失。在很多情况下,显示一个月中的几天并为用户选择一天提供一些选项是有益的。

一个解决方案是使用第三方组件。第二个是由你自己

检查,以实现一个这些链接,将有助于你

http://w2davids.wordpress.com/android-simple-calendar/

http://caughtinthemobileweb.wordpress.com/2011/06/20/how-to-implement-calendarview-in-android/

+0

第二个示例代码是我想要的。谢谢 – Nishant 2012-04-26 03:40:18

2

使用网格布局具有线性布局报头。您可以使用GridLayout实现点击命令,并使用一些奇特的适配器工作,您可以管理一周/月的日子。

编辑:

这里是你可以做什么和GridView将如何为你工作一个粗略的概念。

class CalendarView extends LinearLayout { 

    public CalendarView (Context context) { 
     super(context); 
     setOrientation(LinearLayout.VERTICAL); 

     mHeader = new LinearLayout(context); 
     mHeader.setOrientation(LinearLayout.HORIZONTAL); 
     addView(mHeader); 
     // Add in your days, shouldn't be too bad, they are just text views. 

     mCalendar = new GridView(context); 
     mCalendar.setColumns(mHeader.getNumViews()); // You could hard code this. 
     mCalendar.setAdapterView(new CalendarAdapter()); 
     addView(mCalendar); 
    } 

    // ... Other contructors 


    private class CalendarAdapter extends BaseAdapter { 
     // Override methods, this should be too bad. 

     @Override public view getView (int pos, View convert, ViewGroup parent) { 
      ListView lv = (ListView)convert; 
      if (convert == null) 
       lv = new ListView(parent.getContext()); 

      // Here you will need to figure out some way of 
      // determining the date. 

      CalendarDate app = (CalendarDate)lv.getTag(); 

      // Determine if this view is already set to the correct date, 
      // if not rest the list view 

      app.sort(); 

      lv.setAdapter(new ArrayAdapter(parent.getContext(), R.layout.datelistview, app.getDates()); 
     } 
    } 

    public static class CalendarDate { 
     List<Appointment> mDates = new ArrayList<Appointment>(); 

     public void addAppointment(Appointment app) { 
      mDate.add(app); 
     } 

     // ... and the rest of your methods (getters and state returns) 
    } 

    public Appointment implements Compareable<Appointment> { 
     private Date mDate; 

     private String mName; // Appointment name 
     private String mDesc; // Appointment description 

     @Override public int compareTo(Appointment to) { 
      return mDate.compareTo(mDate); 
      } 
    } 
} 
+0

感谢您的答案哥们。我正在寻找一个日历,我可以在日历的日期标记一些事件。我从Avi Kumar Manku提供的链接中获得了示例代码,这些链接为我提供了我正在寻找的解决方案。你非常支持,感谢很多。 – Nishant 2012-04-26 04:01:02

0

你可以把水平的LinearLayout成垂直LinearLayout与所有textview具有相同的权重。

android:layout_weight="1" 

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

    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="sun" /> 

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

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="tue" /> 

     <TextView 
      android:id="@+id/textView4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="wed" /> 

     <TextView 
      android:id="@+id/textView5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="thu" /> 

     <TextView 
      android:id="@+id/textView6" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="fri" /> 
     <TextView 
      android:id="@+id/textView7" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="sat" /> 
    </LinearLayout> 
    <LinearLayout 
     android:id="@+id/linearLayout1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" > 

     <TextView 
      android:id="@+id/textView1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="sun" /> 

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

     <TextView 
      android:id="@+id/textView3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="tue" /> 

     <TextView 
      android:id="@+id/textView4" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="wed" /> 

     <TextView 
      android:id="@+id/textView5" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="thu" /> 

     <TextView 
      android:id="@+id/textView6" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="fri" /> 
     <TextView 
      android:id="@+id/textView7" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="1" 
      android:text="sat" /> 
    </LinearLayout> 

</LinearLayout> 
+0

请参阅我对Agarwal答案的评论。 – AedonEtLIRA 2012-04-25 14:43:18