Add view programmatically to RelativeLayout android

  • Replies:1
  • OpenNot stickiedUnanswered

Jan 27, 2014 12:49:57 PM

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

For example, to reflect the following picture:



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

1private void addChild(){
2 RelativeLayout relativelayout = (RelativeLayout)findViewById(R.id.relativelayout_imageview_textviews_slider);
3
4 RelativeLayout.LayoutParams textparams = new RelativeLayout.LayoutParams(
5 RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
6
7 textparams.setMargins(0, 25, 5, 0);
8
9 int temp = 0 ;
10 for(int i = 0 ; i < 9 ; ++i){
11 if(i==0){
12
13 ImageButton btn = new ImageButton(getApplicationContext());
14 btn.setId(i+40);
15 btn.setImageResource(R.drawable.ic_launcher);
16 TextView txt = new TextView(getApplicationContext());
17 txt.setText("text"+i);
18 txt.setId(i+40);
19
20 RelativeLayout.LayoutParams imageparams = new RelativeLayout.LayoutParams(
21 RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
22 imageparams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
23 imageparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
24 imageparams.setMargins(0, 5, 0, 0);
25 temp = btn.getId() ;
26 relativelayout.addView(btn,imageparams);
27 textparams.addRule(RelativeLayout.ALIGN_TOP, btn.getId());
28 textparams.addRule(RelativeLayout.LEFT_OF, btn.getId());
29 relativelayout.addView(txt, textparams);
30 }
31 else{
32
33 ImageButton btn = new ImageButton(getApplicationContext());
34 btn.setId(i+1);
35 btn.setImageResource(R.drawable.ic_launcher);
36 TextView txt = new TextView(getApplicationContext());
37 txt.setText("text"+i);
38 txt.setId(i+1);
39
40 RelativeLayout.LayoutParams imageparams = new RelativeLayout.LayoutParams(
41 RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
42 imageparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
43 imageparams.addRule(RelativeLayout.BELOW, temp);
44 imageparams.setMargins(0, 5, 0, 0);
45 temp = btn.getId() ;
46 relativelayout.addView(btn,imageparams);
47 textparams.addRule(RelativeLayout.ALIGN_TOP, btn.getId());
48 textparams.addRule(RelativeLayout.LEFT_OF, btn.getId());
49 relativelayout.addView(txt, textparams);
50 }
51
52 }
53
54 }

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

1<ScrollView
2 android:layout_width="wrap_content"
3 android:layout_height="200dp"
4 >
5 <com.example.TransparentSlidingDrawer
6 android:id="@+id/popup"
7 android:layout_width="wrap_content"
8 android:layout_height="wrap_content"
9 android:orientation="vertical"
10 >
11 <RelativeLayout
12 android:id="@+id/relativelayout_imageview_textviews_slider"
13 android:layout_width="wrap_content"
14 android:layout_height="wrap_content"
15 >
16
17 </RelativeLayout>
18
19
20 </com.example.TransparentSlidingDrawer>
21
22 </ScrollView>

result my function :