gin以编程方式不适用于RelativeLayoutParams

gin以编程方式不适用于RelativeLayoutParams

本文介绍了SetMargin以编程方式不适用于RelativeLayoutParams的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在以编程方式添加封装在Linearlayout中的RelativeLayout.该基地是一个滚动视图,我正在尝试将这些布局添加到名为svbase的滚动视图

I'm currently programmatically adding RelativeLayout encapsulated in a Linearlayout. The base is a scrollview and i'm trying to add those layouts into scrollview named svbase

LinearLayout llbase = new LinearLayout(getApplicationContext());
 LinearLayout.LayoutParams llbaseParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); // Verbose!
llbaseParams.setMargins(0, new CommonOpearation().getPixel(10, getApplicationContext()), 0,  new CommonOpearation().getPixel(10, getApplicationContext()));

llbase.setLayoutParams(llbaseParams);
llbase.setGravity(Gravity.CENTER_HORIZONTAL);

for(int n =0;n<2;n++)
{

  RelativeLayout rLayoutBase = new RelativeLayout(getApplicationContext());
  RelativeLayout.LayoutParams rLayoutParms = new RelativeLayout.LayoutParams( new CommonOpearation().getPixel(140, getApplicationContext()), new CommonOpearation().getPixel(125, getApplicationContext()));
  **rLayoutParms.setMargins(0, 0,  new CommonOpearation().getPixel(5, getApplicationContext()), 0);**
  rLayoutBase.setLayoutParams(rLayoutParms);


  Drawable drawable = MyCurrentActivity.this.getApplicationContext().getResources().getDrawable(R.drawable.curved_bg); //new Image that was added to the res folder
  try {
      rLayoutBase.getClass().getMethod(android.os.Build.VERSION.SDK_INT >= 16 ? "setBackground" : "setBackgroundDrawable", Drawable.class).invoke(rLayoutBase, drawable);
  } catch (Exception ex) {

  }

  llbase.addView(rLayoutBase);
}

  svBase.addView(llbase);

如您所见,我在线性布局中封装了两个水平布局的相对布局.我尝试使用具有一定5dp权限的setMargin为每个relativelayout赋予边距.但是,它不会在两个相对布局之间提供边距.如果我要在xml中手动进行操作,它会起作用.

As you can see I have two relativelayout encapsulated in the linearlayout with a horizontal orientation. I've tried giving margin to each of the relativelayout using setMargin with a right of certain 5dp. However it does not gives margin inbetween the two relativelayout. It worked if I were to do in manually in xml though.

可以在图像上看到差异.顶部是一个xml指定布局,而底部两个是通过编程生成的

The differences can be seen at the image. the top is a xml specify layout while the bottom two relativelayout are generated programmatically

推荐答案

解决了我自己的问题.

解决方案已经存在!谷歌还不够!我的错!相对布局,忽略setMargin()

The solution is already out there! did not google enough! my fault!Relative Layout ignoring setMargin()

这篇关于SetMargin以编程方式不适用于RelativeLayoutParams的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 16:36