简体   繁体   English

以编程方式将视图添加到RelativeLayout android

[英]Add view programmatically to RelativeLayout android

how can I do to add child view programmatically to RelativeLayout at a given position? 如何在给定位置以编程方式将子视图添加到RelativeLayout?

For example, to reflect the following picture: 例如,要反映以下图片:

在此处输入图片说明

my xml: (com.example.TransparentSlidingDrawer child is a simple class that extends with LinearLayout) 我的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"
    />


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

}

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): 您需要对ImageButton进行相同的操作-除了将第一个与父对象对齐,然后再将后一个与上一个ImageButton对齐(以下是伪代码):

    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”中上一个按钮的引用,例如:

btnAbove = btn; btnAbove = btn;

Each view in the layout requires its own LayoutParams object. 布局中的每个视图都需要有自己的LayoutParams对象。 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. 另外,最好使用UI窗口小部件的活动上下文来正确应用您的应用主题。

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 而不是创建所有这些其他参数,而是要注意布局中的位置

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM