【问题标题】:Give ALIGNMENT to the fields in relative layout at Runtime in android?在android的运行时给相对布局中的字段对齐?
【发布时间】:2012-09-12 20:31:48
【问题描述】:

我正在运行时创建字段,例如在相对布局中,我在右上角添加一个文本字段,在左角添加一个复选框。 对于这个问题,目前我正在使用以下代码:

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);

【问题讨论】:

标签: android alignment


【解决方案1】:

*

/**
     * 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);
        }
    }

*

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多