Android日历(caldroid)创建2个日历视图

时间:2016-11-02 21:43:16

标签: android calendar caldroid

您好我使用Caldroid日历,我只使用官方网站的初始化代码我使用getFragmentManager()代替getSupportFragmentManager(),因为我使用的是从Fragment而不是Activity扩展的类中的代码。 MysTab是TabbedActivity的片段。结果我有2个日历,我不知道为什么。有什么想法吗?

截图

enter image description here

public class MysTab extends Fragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.logs_tab, container, false);

        CaldroidFragment caldroidFragment = new CaldroidFragment();
        Bundle args = new Bundle();
        Calendar cal = Calendar.getInstance();
        args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
        args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
        caldroidFragment.setArguments(args);

        FragmentTransaction t = getFragmentManager().beginTransaction();
        t.replace(R.id.caldroidCal, caldroidFragment);
        t.commit();

        return rootView;
    }
}

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <fragment
        android:id="@+id/caldroidCal"
        android:name="com.roomorama.caldroid.CaldroidFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentEnd="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

您需要决定是否要添加动态日历或使用布局为您提供的内容。

  1. 如果您想添加动态日历,那么您的代码应如下所示:

    public class MysTab extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
               return inflater.inflate(R.layout.logs_tab, container, false);
        }
    
        @Override
        public void onViewCreated(View view, Bundle savedInstanceState) {
               CaldroidFragment caldroidFragment = new CaldroidFragment();
    
               Bundle args = new Bundle();
               Calendar cal = Calendar.getInstance();
               args.putInt(CaldroidFragment.MONTH, cal.get(Calendar.MONTH) + 1);
               args.putInt(CaldroidFragment.YEAR, cal.get(Calendar.YEAR));
               caldroidFragment.setArguments(args);
    
               FragmentTransaction t = getChildFragmentManager().beginTransaction();
               t.replace(R.id.caldroidCal, caldroidFragment);
               t.commit();
        }
    
    }
    

    同时你的布局应如下所示:

       <LinearLayout
               xmlns:android="http://schemas.android.com/apk/res/android"
               xmlns:tools="http://schemas.android.com/tools"
               android:layout_width="match_parent"
               android:layout_height="match_parent"
               android:paddingBottom="@dimen/activity_vertical_margin"
               android:paddingLeft="@dimen/activity_horizontal_margin"
               android:paddingRight="@dimen/activity_horizontal_margin"
               android:paddingTop="@dimen/activity_vertical_margin">
    
               <FrameLayout
                     android:layout_width="match_parent"
                     android:layout_height="match_parent"
                     android:id="@+id/caldroidCal" />
    
       </LinearLayout>
    
  2. 如果您想要添加一个静态的日历片段,那么您只需创建一个片段并为您的布局充气。无需手动创建片段:

    public class MysTab extends Fragment {
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
               return inflater.inflate(R.layout.logs_tab, container, false);
        }
    
    }
    

    布局看起来像这样:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin">
    
        <fragment
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:name="com.roomorama.caldroid.CaldroidFragment"
            android:id="@+id/caldroidCal" />
    
    </LinearLayout>