0

嗨,我正在使用 Caldroid 日历,我只使用来自官方网站的初始化代码getFragmentManager()因为getSupportFragmentManager()我在类中使用从 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>
4

1 回答 1

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>
    
于 2016-11-02T22:32:41.567 回答