在我的片段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);
}
}
}
}
以下是应包含其他片段的片段的XML
<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>
这是我想要扩展到我的daysContainer相对布局
的片段的xml <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 :(得分:0)
尝试将ids
向上移动1
。因此,您的第一个id
不等于0
。我的猜测是BELOW
如果给定id
等于0
,则无效。
更新我的评论。
在RelativeLayout
来源中找到以下内容:
rules[BELOW] = a.getResourceId(attr, 0);
0
是默认值,因此会被忽略。