0

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

3 回答 3

0

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

于 2012-11-28T15:31:16.083 回答
0

ARelativeLayout从上到下评估它的孩子。所以,如果你使用类似的东西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>

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

于 2012-11-28T15:13:13.293 回答
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子类通常包含布局属性,如宽度/高度、边距、布局定位规则等。此外,您可以通过删除所有这些包装器RelativeLayoutsTextViews直接在 parent 中设置来改进布局文件RelativeLayout。如果行数很大,您可能还想看看ListViewgalex 在他的回答中所说的小部件。

于 2012-11-28T16:37:37.863 回答