Android 将自定义视图添加到相对布局

Android 将自定义视图添加到相对布局,android,Android,我正在将图像动态添加到相对布局中,如下所示: RelativeLayout relUsers= (RelativeLayout) findViewById(R.id.RelativeLayoutUsers); lpIm = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT); lpIm.le

我正在将图像动态添加到相对布局中,如下所示:

RelativeLayout relUsers= (RelativeLayout) findViewById(R.id.RelativeLayoutUsers);

            lpIm = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
            lpIm.leftMargin = x;
            lpIm.topMargin = y;

            ImageView imageView = new ImageView(this);
            imageView.setImageResource(R.drawable.dot_red);

            relUsers.addView(imageView, lpIm);

但我需要在图像下有一些文本视图,以便我可以更改。我是否需要创建扩展线性布局(或相对布局)的自定义类?如何创建自定义类或视图?

您可以创建一个TextView实例,然后将属性“layout_below”设置为该实例。请记住为ImageView设置一个ID,因为如果不这样做,下面的布局_将无效,它与视图的ID一起工作

lpIm.addRule(RelativeLayout.BELOW, R.id.image_view)
您必须为ImageView设置相同的ID,否则它将无法工作。 谢谢你的评论,复制粘贴