循环LayoutInflater时,每个视图都粘在一起

时间:2012-11-28 13:49:04

标签: android xml view layout-inflater

我有布局问题。我的代码有一个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>

这是打印屏幕:

enter image description here

这是循环:

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);
        }

3 个答案:

答案 0 :(得分:0)

RelativeLayout从上到下评估它的子项。因此,如果您使用android:layout_above="@+id/table_date"之类的内容,则必须使用table_date

<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_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" />

    <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" />

</RelativeLayout>

在这里,我已经交换了文本视图。

答案 1 :(得分:0)

看起来像是一个列表,请查看ListView。 如果正确实现适配器,它甚至会回收视图!

答案 2 :(得分:0)

LayoutParams循环结束时为您的虚增视图设置一些for边距:

    //...
    table_subject.setText(subj[i]);
    table_lecturer.setText(lect[i]);

    // set some proper LayoutParams for the inflated View which is going to be added
    // to the ll LinearLayout
    LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    // set some margins to distance the rows 
    lp.topMargin = 100; 
    lp.bottomMargin = 100; 
    // add the inflated view with to ll with the LayoutParams above.
    ll.addView(perClassTable, lp);
}

LayoutParams是一个特殊类,旨在与ViewGroup子类一起使用,它通常包含布局属性,如宽度/高度,边距,布局定位规则等。 此外,您可以通过删除所有这些包装RelativeLayouts来改进布局文件,并直接在父TextViews中设置RelativeLayout。如果行数很大,你可能还想查看ListView小部件,正如galex在他的回答中所说。