2012-04-25 71 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