单选按钮文本以编程方式位于按钮顶部

时间:2017-12-07 09:16:48

标签: android android-layout radio-button radio-group

有没有办法以编程方式将单选按钮的文本对齐到下面。

enter image description here

我使用以下代码创建广播组

  final RadioButton[] rb = new RadioButton[5];
            RadioGroup rg = new RadioGroup(getActivity()); //create the RadioGroup
            rg.setOrientation(RadioGroup.VERTICAL);//or RadioGroup.VERTICAL
            rg.setLayoutParams(radioparams);

            for (int i = 0; i < 5; i++) {
                rb[i] = new RadioButton(getActivity());
                rb[i].setText("Radiobtn " + i);
                rb[i].setId(i + 100);
                rg.addView(rb[i]);

            }
            layout.addView(rg);

但是我将文本放在每个按钮的右侧。

2 个答案:

答案 0 :(得分:4)

/projects/current/progress

答案 1 :(得分:1)

尝试这种方式你可以做一个技巧

使你的布局像这样

<LinearLayout
    android:id="@+id/lnrView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="80dp"
    android:orientation="horizontal">


</LinearLayout>

而不是像这样添加程序RadioButton

    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            LinearLayout layout = new LinearLayout(MainActivity.this);

            layout.setOrientation(LinearLayout.HORIZONTAL);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);


            LinearLayout layout2 = new LinearLayout(MainActivity.this);
            layout2.setOrientation(LinearLayout.VERTICAL);
            LinearLayout.LayoutParams lp2 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);


            final ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            final TextView textView = new TextView(MainActivity.this);
            int id = 0;
            textView.setId(id);
            textView.setTextSize(14);
            textView.setTextColor(Color.rgb(0, 0, 0));
            textView.setMaxEms(2);
            textView.setText("NIlu");
            layout2.addView(textView);

            RadioButton radioButton = new RadioButton(MainActivity.this);
            layout2.addView(radioButton);
            layout.setLayoutParams(lp);

            lnrView.addView(layout2);


        }
    });