如何在relativelayout中以编程方式在LAYOUT LEFT OF =“+ / id”处设置子视图

时间:2014-01-30 04:04:19

标签: android android-layout android-radiogroup

如图所示我需要设置文本视图LEFT OF RADIO GROUP并且我使用java代码完成....所以我不知道在java代码中为LAYOUT LEFT OF =“”设置参数... ..如何解决它..请用这种方式指导我......

enter image description here

这是类文件......

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.rate_me_up);
    textViewShowTime = (TextView) findViewById(R.id.tvTimeCount);
    layout = (RelativeLayout) findViewById(R.id.linear);

    Button btnp = (Button) findViewById(R.id.buttonp);
    btnp.setOnClickListener(this);

}

@Override
public void onClick(View v) {
    RadioGroup radioGroup = new RadioGroup(Rate_me_up.this);
    radioGroup.setOrientation(0);

    RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    if (mLastRadioGroup != null)
        p.addRule(RelativeLayout.BELOW, mLastRadioGroup.getId());

    radioGroup.setId(mRadioGroupId++);
    mLastRadioGroup = radioGroup;
    layout.addView(radioGroup, p);
    RadioButton radioButtonView = new RadioButton(Rate_me_up.this);
    radioButtonView.setText("radio1");
    radioButtonView.setId(id++);
    radioButtonView.setButtonDrawable(R.drawable.e404);
    radioButtonView.setChecked(false);
    radioGroup.addView(radioButtonView, p);

    RadioButton radioButtonView2 = new RadioButton(Rate_me_up.this);
    radioButtonView2.setText("radio2");
    radioButtonView2.setId(id++);
    radioButtonView2.setChecked(false);
    radioGroup.addView(radioButtonView2, p);
    RadioButton radioButtonView3 = new RadioButton(Rate_me_up.this);
    radioButtonView3.setText("radio3");
    radioButtonView3.setId(id++);
    radioGroup.addView(radioButtonView3, p);
    radioButtonView3.setChecked(false);
    TextView txt = new TextView(Rate_me_up.this);

    txt.setText("FOOD QUALITY!");
    layout.addView(txt, p);

    radioGroup.setOnCheckedChangeListener(this);

}

2 个答案:

答案 0 :(得分:1)

addRule 执行类似的操作..

 RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
 params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
 params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
 button.setLayoutParams(params);

答案 1 :(得分:1)

好的,你可以这样做:

TextView txt = new TextView(Rate_me_up.this);
txt.setText("FOOD QUALITY!");
RelativeLayout.LayoutParams params=
         new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
               LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.LEFT_OF,radiofroup.getId());
txt.setLayoutParams(params);
layout.addView(txt);

希望我回答你的问题。 :)