【问题标题】:Programmatically Inflated RadioButton layout_width does not match the layout file以编程方式膨胀的 RadioButton layout_width 与布局文件不匹配
【发布时间】:2016-03-04 14:42:54
【问题描述】:

我正在以编程方式添加 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);

【问题讨论】:

    标签: android android-layout radio-button


    【解决方案1】:

    在添加视图之前使用它:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-02
      • 1970-01-01
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多