RelativeLayout.LayoutParams,setMargins不起作用

时间:2013-06-23 21:15:50

标签: android android-layout

我尝试按屏幕大小设置文本之间的边距。

除了一个TextView之外它还能正常工作。

只有mon右边距不起作用。

感谢您的帮助:)

以下是代码和图片:

public void setDaysBar()
    {

    Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

    if (android.os.Build.VERSION.SDK_INT >= 13)
      {
          Point size = new Point();
          display.getSize(size);
          width = size.x;
      }
      else
          width = display.getHeight();  

    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);        
    lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
    sun.setLayoutParams(lp);

    lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);        
    lp.addRule(RelativeLayout.RIGHT_OF, R.id.textView3);        
    lp.setMargins(0, 0, width/6, 0);
    mon.setLayoutParams(lp);

    lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);        
    lp.addRule(RelativeLayout.RIGHT_OF, R.id.textView2);
    lp.setMargins(0, 0, width/6, 0);
    tue.setLayoutParams(lp);

    lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);        
    lp.addRule(RelativeLayout.RIGHT_OF, R.id.textView9);
    lp.setMargins(0, 0, width/6, 0);
    wed.setLayoutParams(lp);

    lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);        
    lp.addRule(RelativeLayout.RIGHT_OF, R.id.textView1);
    lp.setMargins(0, 0, width/6, 0);
    thu.setLayoutParams(lp);

    lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);        
    lp.addRule(RelativeLayout.RIGHT_OF, R.id.textView4);
    lp.setMargins(0, 0, width/6, 0);
    fri.setLayoutParams(lp);

    lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    lp.addRule(RelativeLayout.RIGHT_OF, R.id.textView6);
    lp.setMargins(0, 0, width/6, 0);
    sat.setLayoutParams(lp);

    }

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为你周日也应该setMargins() - 看起来你已经忘记了它。
如果你这样做,一切都应该工作得很好。

RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);        
lp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
lp.setMargins(0, 0, width / 6, 0);
sun.setLayoutParams(lp);