以编程方式膨胀的RadioButton layout_width与布局文件不匹配

时间:2016-03-04 06:21:31

标签: android android-layout radio-button

我以编程方式添加RadioButtons:

RadioButton radioButton = (RadioButton) inflater.inflate(R.layout.radio_button, null);
...
radioGroup.addView(radioButton);

在RadioButton布局文件中,宽度为match_parent:

         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:button="@null"
         android:drawableLeft="@drawable/check_selector"
         android:drawablePadding="@dimen/large"
         android:paddingBottom="@dimen/large"
         android:paddingTop="@dimen/large"
         android:textColor="@drawable/text_selector"
然而,在将它添加到RadioGroup之后,它的宽度是wrap_content:" width:-2"

Timber.e("width: %s", radioGroupReason.getChildAt(radioGroupReason.getChildCount() -1).getLayoutParams().width);

1 个答案:

答案 0 :(得分:4)

在添加视图之前使用它:

radioButton.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));