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

Android calendar (caldroid) create 2 calendar views

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

截图

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. 如果您想添加动态日历,那么您的代码应如下所示:

    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>