2016-12-14 54 views
0

在我的片段的onCreateView方法我想以編程方式膨脹下相互viewstubs到佈局,但前兩個發生在彼此內的地方,like this試圖建立在彼此的看法編程的片段

這裏是我的片段的onCreateView方法

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

    mBaseLayout = (PercentRelativeLayout) inflater.inflate(R.layout.fragment_calender, container, false); 
    findViews(); 
    mBundle = new Bundle(); 

    mFragments = new ArrayList<>(); 
    mDaysContainer = (RelativeLayout) mBaseLayout.findViewById(R.id.daysContainer); 



    mStartPeriod = DateAndTime.getFirstDayFirstWeekOfMonthAsString("yyyy-MM-dd"); 
    mEndPeriod = DateAndTime.getLastDayLastWeekOfMonthAsString("yyyy-MM-dd"); 

    mPeriodTextView.setText(mStartPeriod + " - " + mEndPeriod); 

    createDayFragments(); // Create the fragments 
    findDayViewIds();  // Get access to the views 

    return mBaseLayout; 
} 

我用這個方法來創建的意見,並把它們放入容器

private void createDayFragments() { 
    for (int i = 0; i < 30; i++) { 
     mFragments.add(new ViewStub(getActivity())); 
     mFragments.get(i).setId(i); 
     mDaysContainer.addView(mFragments.get(i)); 
     ViewStub viewStub = (ViewStub) mBaseLayout.findViewById(i); 
     viewStub.setInflatedId(i); 
     viewStub.setLayoutResource(R.layout.fragment_day); 
     Log.d("ID", mFragments.get(i).getId() + ""); 
     Log.d("i = ", i + ""); 

     RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 

     if(i >= 1){ 
      p.addRule(RelativeLayout.BELOW, i - 1); 
      Log.d("Below ID", i - 1 + ""); 
     } 


     viewStub.setLayoutParams(p); 

     Log.d("", ""); 
     viewStub.inflate(); 
    } 
} 

我用這個方法創建它們後,可以訪問所有的意見

private void findDayViewIds(){ 
    for(int i = 0; i < 30; i++) { 
     RelativeLayout tmpRel = (RelativeLayout) mDaysContainer.findViewById(i); 
     for(int j = 0; j < tmpRel.getChildCount(); ++j){ 
      if(tmpRel.getChildAt(j) instanceof PercentRelativeLayout) { 
       PercentRelativeLayout percentTmp = (PercentRelativeLayout) tmpRel.getChildAt(j); 
       percentTmp.setId(100 + i); 
       for(int h = 0; h < percentTmp.getChildCount(); ++h){ 
        if(percentTmp.getChildAt(h) instanceof TextView){ 
         TextView textViewTmp = (TextView) percentTmp.getChildAt(h); 
         textViewTmp.setId(300 + i); 
         textViewTmp.setText("Day " + i + " ID: " + tmpRel.getId()); 
        } 
       } 
      } else if (tmpRel.getChildAt(j) instanceof ViewStub){ 
       tmpRel.getChildAt(j).setId(200 + i); 
      } 
     } 
    } 
} 

下面是應該包含其他片段

<android.support.percent.PercentRelativeLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/weekDisplayed"> 

<TextView 
    android:text="Placeholder" 
    android:layout_alignParentTop="true" 
    android:gravity="center" 
    app:layout_heightPercent="10%" 
    app:layout_widthPercent="94%" 
    android:layout_centerHorizontal="true" 
    android:background="@drawable/datefrom_dateto_bottom_border" 
    android:id="@+id/periodTextView"/> 


    <ScrollView 
     android:id="@+id/scrollView" 
     app:layout_widthPercent="94%" 
     android:layout_below="@+id/periodTextView" 
     android:layout_centerHorizontal="true" 
     app:layout_marginBottomPercent="4%" 
     android:layout_width="match_parent"> 

    <RelativeLayout <!-- This is where i want the fragments --> 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:id="@+id/daysContainer"> 

     </RelativeLayout> 

</ScrollView> 

</android.support.percent.PercentRelativeLayout> 

片段中的XML這裏是對的XML片段我想誇大了我的daysContainer相對佈局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content"> 



<android.support.percent.PercentRelativeLayout 
     android:gravity="center_vertical" 
     android:id="@+id/dayContainerOne" 
     android:layout_width="match_parent" 
     android:layout_height="35dp" 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:app="http://schemas.android.com/apk/res-auto"> 


     <TextView 
      app:layout_marginLeftPercent="2%" 
      android:text="placeholder" 
      android:id="@+id/weekOneTextView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:typeface="monospace"/> 

    </android.support.percent.PercentRelativeLayout> 

    <ViewStub layout="@layout/fragment_replace" 
       android:id="@+id/replaceOne" 
       android:layout_below="@id/dayContainerOne" 
       android:layout_height="wrap_content" 
       android:layout_width="wrap_content"> 

    </ViewStub> 
</RelativeLayout> 
+0

嘗試將'ids'向上移動1。所以你的第一個'id'不等於0.我的猜測是,如果'id'等於'0','BELOW'不起作用。 –

回答

0

嘗試通過1轉移ids起來。所以你的第一個id不等於0。我的猜測是,BELOW不起作用,如果給予id等於0

更新我的評論。

實測值以下行中RelativeLayout來源:

rules[BELOW] = a.getResourceId(attr, 0);

0是默認值,因此它被忽略。