1

how can I do to add child view programmatically to RelativeLayout at a given position?

For example, to reflect the following picture:

enter image description here

my xml: (com.example.TransparentSlidingDrawer child is a simple class that extends with LinearLayout)

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:orientation="horizontal" >
 <ScrollView 
        android:layout_width="wrap_content"
        android:layout_height="100dp"
        >
<com.example.TransparentSlidingDrawer
    android:id="@+id/popup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
     >
     <RelativeLayout 
         android:id="@+id/relativelayout_imageview_textviews_slider"
         android:layout_width="wrap_content"
         android:layout_height="100dp"
         >    
     </RelativeLayout>


</com.example.TransparentSlidingDrawer>

</ScrollView> 
<ImageButton 
    android:id="@+id/open_close_slider"
    android:layout_width="25dp"
    android:layout_height="25dp"
    />


this is my function that this function does not work well :

private void addChild(){
    RelativeLayout relativelayout = (RelativeLayout)findViewById(R.id.relativelayout_imageview_textviews_slider);

    RelativeLayout.LayoutParams imageparams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    imageparams.setMargins(0, 5, 0, 0);

    RelativeLayout.LayoutParams textparams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

    textparams.setMargins(0, 25, 5, 0);

    imageparams.addRule(RelativeLayout.ALIGN_PARENT_TOP,RelativeLayout.TRUE);
    imageparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);

    for(int i = 0 ; i < 9 ; ++i){
        ImageButton btn = new ImageButton(getApplicationContext());
        btn.setId(i+1);
        btn.setBackgroundResource(R.drawable.ic_launcher);
        TextView txt = new TextView(getApplicationContext());
        txt.setText("text"+i);
        txt.setId(i+1);
        textparams.addRule(RelativeLayout.ALIGN_TOP, btn.getId());
        textparams.addRule(RelativeLayout.LEFT_OF, btn.getId());
        relativelayout.addView(btn, imageparams);
        relativelayout.addView(txt, textparams);
    }

}
user3103823
  • 135
  • 1
  • 3
  • 13

3 Answers3

1

You are aligning the textviews with button tops and lefts here:

    textparams.addRule(RelativeLayout.ALIGN_TOP, btn.getId());
    textparams.addRule(RelativeLayout.LEFT_OF, btn.getId());

You need to do the same for the ImageButtons - except align the first one with the parent, then each subsequent one you need to align the top to the previous ImageButton (here is fake code):

    btn.addRule(RelativeLayout.ALIGN_BOTTOM, btnAbove.getId());

Then keep a reference to the previous button in "btnAbove" at the end of your loop like:

btnAbove = btn;

Jim
  • 10,172
  • 1
  • 27
  • 36
  • ok,now,Where is my problem? http://paste.ubuntu.com/6824994/ http://i.stack.imgur.com/g8Ckw.jpg – user3103823 Jan 27 '14 at 08:16
  • you are telling the textviews to fill the parent.. they should probably WRAP_CONTENT not your code here -> RelativeLayout.LayoutParams textparams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.FILL_PARENT (problem), RelativeLayout.LayoutParams.FILL_PARENT (problem)); – Jim Jan 27 '14 at 08:29
  • I changed it to warp_content but I have the same problem earlier.(http://i.stack.imgur.com/g8Ckw.jpg) – user3103823 Jan 27 '14 at 11:11
  • what is the problem? Also, you may need to create new params with each object - you keep adding rules to the same ones – Jim Jan 27 '14 at 13:18
0

Each view in the layout requires its own LayoutParams object. Reusing the same object won't work the way you want.

Also, prefer using an activity context for UI widgets to get your app theme properly applied.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

You can also do this

final TextView titleView = (TextView) itemLayout
            .findViewById(R.id.titleView);

Rather than creating all those additional parameters this will take care of the position in the layout

RandomGuy
  • 81
  • 4