RelativeLayout的AddRule不起作用

时间:2018-02-14 13:52:16

标签: android android-relativelayout

我正在尝试以编程方式将相对布局添加到线性布局。对于相对布局,我以编程方式添加了imageview和textview。我添加了规则,使文本视图位于imageview的顶部,右下角。但是addrule似乎没有用.Below是我的代码。

 RelativeLayout relLyt=new RelativeLayout(mContext);
                    TextView textView=new TextView(mContext);
                    textView.setBackgroundColor(Color.parseColor("#59000000"));
                    textView.setTextColor(Color.WHITE);
                    textView.setTextSize(14f);
                    textView.setText("3:20");
                    textView.setPadding(15,10,15,10);

                    final ImageView myImage = new ImageView(mContext);
                    myImage.setTag(i);

                    //myImage.setOnClickListener(this);
                     myImage.setId(i);
                    myImage.setImageDrawable(mContext.getResources().getDrawable(R.mipmap.ic_launcher));


                    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);

                    lp.height = height ;
                    lp.width = width/2 ;

                    lp.setMargins(2, 2, 2, 2);



                    myImage.setScaleType(ImageView.ScaleType.FIT_XY);
                    myImage.setLayoutParams(lp);
                    relLyt.setLayoutParams(lp);
                    relLyt.addView(myImage);
                    relLyt.addView(textView);

                    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)textView.getLayoutParams();
                    params.addRule(RelativeLayout.ALIGN_RIGHT,myImage.getId());
                    params.addRule(RelativeLayout.ALIGN_BOTTOM,myImage.getId());
                    params.setMargins(10, 10, 10, 10);
                    textView.setLayoutParams(params);


                    contactViewHolder.img_lyt.addView(relLyt);

0 个答案:

没有答案