【问题标题】:Radio button text to top of the button programmatically以编程方式将单选按钮文本添加到按钮顶部
【发布时间】:2018-05-21 08:23:42
【问题描述】:

有什么方法可以像下面这样以编程方式将单选按钮的文本对齐到顶部。

我使用下面的代码来创建广播组

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

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

【问题讨论】:

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


【解决方案1】:
    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);
        rb[i].setButtonDrawable(null);

        TypedArray a = getTheme().obtainStyledAttributes(R.style.AppTheme, new int[] {android.R.attr.listChoiceIndicatorSingle});
        int attributeResourceId = a.getResourceId(0, 0);
        Drawable drawable = getResources().getDrawable(attributeResourceId);

        rb[i].setCompoundDrawablesWithIntrinsicBounds(null, null, null, drawable);
        rb[i].setGravity(Gravity.CENTER | Gravity.BOTTOM);
        rg.addView(rb[i]);

    }
    layout.addView(rg);

【讨论】:

  • 如何更改单选按钮颜色。 ?
  • DrawableCompat.setTint(drawable, Color.BLUE);
【解决方案2】:

试试这个方法你可以做一个技巧

让你的布局像这样

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


        }
    });

【讨论】:

  • 我只想将单选按钮文字设置为顶部,所以需要使用textview吗?
猜你喜欢
  • 1970-01-01
  • 2020-05-19
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 1970-01-01
  • 1970-01-01
  • 2012-06-19
相关资源
最近更新 更多