Java 以编程方式将单选按钮与右侧的文本和左侧的按钮对齐

Java 以编程方式将单选按钮与右侧的文本和左侧的按钮对齐,java,android,android-layout,android-relativelayout,Java,Android,Android Layout,Android Relativelayout,我试图以编程方式将左侧的单选按钮的文本与右侧的按钮对齐,而不是在下一个屏幕截图中显示的内容 我试着创建一个相对的Layout,并放置textview和radiobtn(没有文本),但每次尝试都没有成功:( 我在这里发布了我试图运行但无法对齐的代码 RelativeLayout relLay = new RelativeLayout(this); relLay.setLayoutParams(new LayoutParams(LayoutParams.FILL_P

我试图以编程方式将左侧的单选按钮的文本与右侧的按钮对齐,而不是在下一个屏幕截图中显示的内容

我试着创建一个相对的Layout,并放置textview和radiobtn(没有文本),但每次尝试都没有成功:(

我在这里发布了我试图运行但无法对齐的代码

        RelativeLayout relLay = new RelativeLayout(this);
        relLay.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

        TextView textView=new TextView(this);
        textView.setText(device.getName());
        textView.setGravity(Gravity.LEFT);  

        RadioButton radioBtn= new RadioButton(this);
        radioBtn.setText(" ");
        radioBtn.setTag(device);
        radioBtn.setChecked(false);
        radioBtn.setGravity(Gravity.RIGHT); 

        RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
        RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

        lp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);

        relLay.addView(textView,lp1);
        relLay.addView(radioBtn,lp2);
        radioGr.addView(relLay);
我还必须指出,我在一个radiogroup中添加了relativelayouts,它本身包含在一个ScrollView中


提前感谢您的帮助和耐心

您可以使用以下代码:

RelativeLayout relLay = new RelativeLayout(this);
            relLay.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
            relLay.setGravity(Gravity.RIGHT);
            TextView textView=new TextView(this);
            textView.setText("good");
            textView.setGravity(Gravity.RIGHT);  
            RadioButton radioBtn= new RadioButton(this);
            radioBtn.setChecked(false);
            radioBtn.setId(125); //you must setid for radioBtn

            RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);

          lp1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
                 RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp1.addRule(RelativeLayout.ALIGN_BASELINE,radioBtn.getId());
          lp1.addRule(RelativeLayout.LEFT_OF,radioBtn.getId());
            lp2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            relLay.addView(radioBtn,lp2);
            relLay.addView(textView,lp1);
            this.setContentView(relLay);