0

I have a layout problem. my code have a LayoutInflater set the following xml as layout, and will loop few time to show my school time table. Eventhough i have set the relativelayout's marginBottom and Top to 50dp, but why every layout still stick together?

here's the 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>

Here is the print screen:

enter image description here

here is the loop:

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);
        }
crossRT
  • 616
  • 4
  • 12
  • 26
  • Can you show how you implement the margins? From what I understand you don't want these boxes touching, you want just a 50dp margin between each? – jnthnjns Nov 28 '12 at 14:17
  • ya, i want to set some space between 2 boxes. Any ideas? =S – crossRT Nov 28 '12 at 14:24
  • Can you post the code with the loop where you use the `LayoutInflater` to inflate that layout file? – user Nov 28 '12 at 14:49
  • okay, i updated~ =) tableRow.size() actually mean how many class is need to show. – crossRT Nov 28 '12 at 14:55
  • Does this snippet of code, at the end of your `for` loop makes a difference? `LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); lp.topMargin = 100; lp.bottomMargin = 100; ll.addView(perClassTable, lp);` – user Nov 28 '12 at 15:35
  • Your code is works very well! Thx @Luksprog is that LinearLayout.LayoutParam mean create a LinearLayout outside the RelativeLayout with the params? – crossRT Nov 28 '12 at 16:17

3 Answers3

0

A RelativeLayout evaluates it's children from top to bottom. So, if you use something like android:layout_above="@+id/table_date", it must come after 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>

Here, I have swapped the textviews.

Simon
  • 14,407
  • 8
  • 46
  • 61
  • Sorry @Simon, maybe my question is not very clear. I mean every rectangle box's top and bottom is touching. =( i need to set some space between them. But thx ya! – crossRT Nov 28 '12 at 15:22
0

Looks like a list to me, look into ListView. It will even recycle views if you implement your adapter correctly!

galex
  • 3,279
  • 2
  • 34
  • 46
0

Set some LayoutParams with some margins for your inflated view at the end of the for loop:

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

The LayoutParams is a special class designed to be use with a ViewGroup subclass which generally holds layout attributes like width/height, margins, layout positioning rules etc. Also, you could improve the layout file by removing all those wrappers RelativeLayouts and set the TextViews directly in the parent RelativeLayout. If the number of rows is big you may also want to have a look at the ListView widget as galex said in his answer.

user
  • 86,916
  • 18
  • 197
  • 190