我有一个布局问题。我的代码有一个 LayoutInflater 将以下 xml 设置为布局,并且会循环几次以显示我的学校时间表。尽管我已将 relativelayout 的 marginBottom 和 Top 设置为 50dp,但为什么每个布局仍然粘在一起?
这是xml:
<RelativeLayout
android:id="@+id/left_part"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<TextView
android:id="@+id/table_day"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_above="@+id/table_date"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="@string/table_day"
android:textSize="@dimen/table_day_size" />
<TextView
android:id="@+id/table_date"
android:layout_width="match_parent"
android:layout_height="14dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="center"
android:text="@string/table_date"
android:textSize="@dimen/table_date_size" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/center_part"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/left_part"
android:layout_alignTop="@+id/left_part"
android:layout_toRightOf="@+id/left_part" >
<RelativeLayout
android:id="@+id/center_part1"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<TextView
android:id="@+id/table_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="@string/table_time"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="@dimen/table_timelocationclass_size" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/center_part2"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/center_part1" >
<TextView
android:id="@+id/table_subject"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:padding="1dp"
android:gravity="center_vertical"
android:text="@string/table_subject"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="@dimen/table_subject_size" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/center_part3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/center_part2" >
<TextView
android:id="@+id/table_lecturer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="@string/table_lecturer"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="@dimen/table_lecturer_size" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/right_part"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/center_part"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/center_part"
android:layout_toRightOf="@+id/center_part" >
<RelativeLayout
android:id="@+id/right_part1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" >
<TextView
android:id="@+id/table_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/table_location" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/right_part2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/right_part1" >
<TextView
android:id="@+id/table_class"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="@string/table_class" />
</RelativeLayout>
</RelativeLayout>
这是打印屏幕:
这是循环:
LinearLayout ll = (LinearLayout)findViewById(R.id.main_table);
ll.removeAllViews();
//set table layout
for(int i=0;i<tableRow.size();i++)
{
LayoutInflater inflater = getLayoutInflater();
View perClassTable = inflater.inflate(R.layout.perclass_table, null);
TextView table_day = (TextView)perClassTable.findViewById(R.id.table_day);
TextView table_date = (TextView)perClassTable.findViewById(R.id.table_date);
TextView table_time = (TextView)perClassTable.findViewById(R.id.table_time);
TextView table_location = (TextView)perClassTable.findViewById(R.id.table_location);
TextView table_class = (TextView)perClassTable.findViewById(R.id.table_class);
TextView table_subject = (TextView)perClassTable.findViewById(R.id.table_subject);
TextView table_lecturer = (TextView)perClassTable.findViewById(R.id.table_lecturer);
table_day.setText(date[i].substring(0, 3));
table_date.setText(date[1].substring(4,13));
table_time.setText(time[i]);
table_location.setText(loca[i]);
table_class.setText(clas[i]);
table_subject.setText(subj[i]);
table_lecturer.setText(lect[i]);
//testing
ll.addView(perClassTable);
}