2012-09-12 44 views
0

我正在努力在运行时创建字段,就像在相对布局中在右上角添加一个文本字段并在左上角添加一个复选框。 对于这个我得到的问题,目前我使用下面的代码:在运行时在Android中给相对布局的字段进行对齐?

ViewGroup hori_layout=new RelativeLayout(getParent()); 
      hori_layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); 

      TextView tv1=new TextView(getParent()); 
      tv1.setText(_medContactNames[i]); 
      tv1.setTextColor(Color.BLACK); 
      CheckBox cb = new CheckBox(getApplicationContext()); 

      hori_layout.addView(tv1); 
      hori_layout.addView(cb); 

      layout.addView(hori_layout); 
+1

看到这个 它会帮助你 [1]: http://stackoverflow.com/questions/3885077/setting-parameters-on-child-views-of-a-relativelayout – Santosh

+0

Thanxxxx很多...我明白了。 。! –

+0

堆栈溢出总是欢迎你...为我的评论投票 – Santosh

回答

0

*

/** 
    * GENERATING RELATIVE LAYOUT AT RUNTIME 
    * */ 
    public class RL extends RelativeLayout { 
     public RL(Context context,int i,String flag) { 
      super(context); 
      //FIRST FIELD OF THE LAYOUT 
      TextView firstField = new TextView(context); 
      firstField.setTextColor(Color.BLACK); 
      if(flag.equalsIgnoreCase("LAW")){ 
       firstField.setText(_lawContactNames[i]); 
      }else{ 
       firstField.setText(_medContactNames[i]); 
      } 
      firstField.setId(1); 
      //SECOND FIELD OF THE LAYOUT 
      CheckBox secondField = new CheckBox(context); 
      secondField.setId(2); 
      //FIRST LAYOUT WHICH MUST BE PRESENT AT LEFT END == TEXT FIELD 
      RelativeLayout.LayoutParams lpSecond = new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      addView(firstField, lpSecond); 
      //SECOND LAYOUT AT RIGHT END == CHECK BOX 
      RelativeLayout.LayoutParams lpFirst = new RelativeLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
      lpFirst.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, secondField.getId()); 
      addView(secondField, lpFirst); 
     } 
    } 

*