android - 以编程方式将 View 添加到 RelativeLayout android

标签 android xml android-layout android-relativelayout

如何在给定位置以编程方式将 subview 添加到 RelativeLayout?

例如要反射(reflect)如下图片:

enter image description here

我的 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"
    />


这是我的功能,这个功能不能很好地工作:

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

}

最佳答案

您在此处将 TextView 与按钮顶部和左侧对齐:

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

您需要对 ImageButton 执行相同的操作 - 除了将第一个与父级对齐,然后每个后续的都需要将顶部与前一个 ImageButton 对齐(这里是假代码):

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

然后在循环结束时保留对“btnAbove”中上一个按钮的引用,例如:

btnAbove = btn;

关于android - 以编程方式将 View 添加到 RelativeLayout android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21369585/

相关文章:

android - 如何跟踪 Amazon SNS 发送的打开的推送通知

android - 如何启动 list 中提到的不存在的 Activity ?

mysql load xml语句产生0条记录

php - 如何获取<yt:accesscontrol>的xml属性?

json - JSON 的 xpath 与 XML 不同

android - 自定义 View 不响应触摸

android - 清晰的图标 清晰的 Android 应用程序

android - 在 aidl(ipc) 中传递自定义数组列表

java - if (ourHolder.getSurface().isValid() 似乎返回 false

android - 按钮不引起 onClick 事件