动态单选按钮不包含wrap_content

时间:2017-09-21 09:47:40

标签: java android android-widget

我创建了一个带有这样的自定义drawable的单选按钮。 enter image description here

我使用了自定义选择器

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/est_status_radio_checked" android:state_checked="true" android:state_enabled="true" />
<item android:drawable="@drawable/est_status_radio_checked" android:state_checked="true" android:state_enabled="false" />
<item android:drawable="@drawable/est_status_radio_unchecked" android:state_checked="false" /></selector>

使用下面的单选按钮

  <style name="AppTheme.StatusRadioButtons">
    <item name="fontPath">fonts/SFUIText-Semibold.ttf</item>
    <item name="android:textSize">10sp</item>
    <item name="android:background">@drawable/establishment_status_selector</item>
    <item name="android:elevation">5dp</item>
    <item name="android:paddingLeft">5dp</item>
    <item name="android:textColor">@drawable/est_status_text_color</item>
    <item name="android:button">@null</item>
</style>

  <RadioButton
                    style="@style/AppTheme.StatusRadioButtons"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="10dp"
                    android:checked="false"
                    android:gravity="center"
                    android:text="Stopped" />

一切正常,但现在要求是创建动态按钮。 出于某种原因,当我创建一个动态单选按钮时,drawable显示为拉伸:@ enter image description here

我已经创建了这样的按钮

private RadioButton createRadioForStatus(EstablishmentStatus establishmentStatus) {
    RadioButton radioButton = new RadioButton(getContext());
    radioButton.setCompoundDrawablePadding(0);
    radioButton.setPadding(0,0,0,0);
    radioButton.setBackground(ContextCompat.getDrawable(getContext(),R.drawable.establishment_status_selector));
    radioButton.setButtonDrawable(null);
    radioButton.setTextSize(10);
    radioButton.setElevation(getResources().getDimension(R.dimen.unit_small));
    radioButton.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));// wrap content buttons are working perfectly in xml but for some motherfucking reason are not wrapping the content properly when done programatically
    radioButton.setText(establishmentStatus.getName());
    return radioButton;
}

我的选择器drawable是这样的

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape xmlns:android="http://schemas.android.com/apk/res/android">
        <solid android:color="@color/colorPrimary" />
        <corners android:radius="30dp" />
    </shape>
</item>
<item
    android:bottom="5dp"
    android:left="5dp"
    android:right="55dp"
    android:top="5dp">
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
        <solid android:color="@color/colorWhite" />
        <size
            android:width="10dp"
            android:height="10dp"></size>
    </shape>
</item>

2 个答案:

答案 0 :(得分:0)

您应该在单独的目录中设置单选按钮的src,然后通过R

找到它

答案 1 :(得分:0)

您的RadioButton应该在同一个RadioGroup内,有类似的内容:

    RadioGroup radioGroup = new RadioGroup(this);
    RadioButton radioButton1= new RadioButton(this);
    RadioButton radioButton2= new RadioButton(this);

    radioGroup.addView(radioButton1);
    radioGroup.addView(radioButton2);

我希望这会有所帮助。