如何在自定义应用程序中使用Android Stock Calendar中的视图(月,日,周)

时间:2014-02-22 08:12:29

标签: android android-view android-xml android-source

在谷歌股票Android日历中存在多个视图(月,周,议程,周),从Google Android Sources Repositories下载的源代码具有相同的应用程序,通过探索名为com.android.calendar的包的代码有负责呈现这些活动的主要观点:

enter image description here

通过努力解决代码,我使用了两个包com.android.calendar.monthcom.android.calendar,但无法实现我的目标,这是我使用的示例:

enter image description here

以及主要的以下代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

这是我的xml代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/month"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:paddingTop="1dip"
    android:background="@null">

    <TextView android:id="@+id/month_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dip"
        android:visibility="gone"
        style="@style/MonthView_MiniMonthLabel" />

    <include android:id="@+id/day_names" layout="@layout/full_month_header" />

     <View
        android:background="@color/calendar_grid_line_inner_vertical_color"
        android:layout_height="1px"
        android:layout_width="match_parent" />

    <com.android.calendar.month.MonthListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:drawSelectorOnTop="false" />

</LinearLayout>

直到这里代码工作正常,但是空白屏幕和操作栏,如果我的问题不清楚,请尝试编辑它。

What should be done to include Calendar Views (Month, Week, Day) in a custom application ?

2 个答案:

答案 0 :(得分:2)

您应该看看CalendarView这是在Android中集成日历的标准方法。

此外,如果您需要选择日期&amp;用户界面中的时间有DatePickerTimePicker

请记住,android中的默认日历应用是开源!随意dig through the code here

答案 1 :(得分:1)

屏幕为空白,因为您尚未为ListView设置适配器。您可以在MonthByWeekFragment中的原始日历应用中查看适配器的配置方式。

    @Override
    protected void setUpAdapter() {
        mFirstDayOfWeek = Utils.getFirstDayOfWeek(mContext);
        mShowWeekNumber = Utils.getShowWeekNumber(mContext);

        HashMap<String, Integer> weekParams = new HashMap<String, Integer>();
        weekParams.put(SimpleWeeksAdapter.WEEK_PARAMS_NUM_WEEKS, mNumWeeks);
        weekParams.put(SimpleWeeksAdapter.WEEK_PARAMS_SHOW_WEEK, mShowWeekNumber ? 1 : 0);
        weekParams.put(SimpleWeeksAdapter.WEEK_PARAMS_WEEK_START, mFirstDayOfWeek);
        weekParams.put(MonthByWeekAdapter.WEEK_PARAMS_IS_MINI, mIsMiniMonth ? 1 : 0);
        weekParams.put(SimpleWeeksAdapter.WEEK_PARAMS_JULIAN_DAY,
            Time.getJulianDay(mSelectedDay.toMillis(true), mSelectedDay.gmtoff));
        weekParams.put(SimpleWeeksAdapter.WEEK_PARAMS_DAYS_PER_WEEK, mDaysPerWeek);
        if (mAdapter == null) {
            mAdapter = new MonthByWeekAdapter(getActivity(), weekParams, mEventDialogHandler);
            mAdapter.registerDataSetObserver(mObserver);
        } else {
            mAdapter.updateParams(weekParams);
        }
        mAdapter.notifyDataSetChanged();
    }

以及如何将适配器添加到SimpleDayPickerFragment中的ListView:

@Override
public void onAttach(Activity activity) {
    ...

    setUpAdapter();
    setListAdapter(mAdapter);
}