如何在給定位置以編程方式將Child View添加到RelativeLayout?以編程方式將視圖添加到RelativeLayout android
例如,以反映如下圖:
我的xml:(com.example.TransparentSlidingDrawer孩子是一個簡單的類,它使用的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"
/>
這是我的功能th在這個功能不能很好地工作:
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);
}
}
什麼是RelativeLayout的自定義的LinearLayout裏面呢? – pskink
以編程方式添加textviews和imageviews。 – user3103823