【问题标题】:Align radiobuttons with text at right and button at left programmatically以编程方式将单选按钮与右侧的文本和左侧的按钮对齐
【发布时间】:2012-11-26 01:09:06
【问题描述】:

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

我尝试创建一个相对布局并放置 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);

我还必须指出,我正在将相对布局添加到由 ScrollView 包含的无线电组中。

提前感谢您的帮助和耐心

【问题讨论】:

    标签: java android android-layout android-relativelayout


    【解决方案1】:

    您可以使用以下代码:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      • 2020-05-06
      • 2019-11-22
      • 2011-12-07
      • 2012-11-19
      • 1970-01-01
      相关资源
      最近更新 更多