2016-11-02 50 views
0

嗨我使用Caldroid日曆,並且我只使用官方網站的初始化代碼,我使用getFragmentManager()而不是getSupportFragmentManager(),因爲我使用的代碼擴展自Fragment not Activity。 MysTab是TabbedActivity的片段。結果我有兩個日曆,我不知道爲什麼。有任何想法嗎?安卓日曆(caldroid)創建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> 

回答

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> 
    
+0

非常感謝:) – Aligator