动态添加时单选按钮文本颜色问题

Radio button text color issue when dynamically added

我在单选组中有一个单选按钮,一个单选组以编程方式创建,一个来自 XML,由 XML 创建的单选按钮具有在文本选择器中定义的文本颜色,但是以编程方式添加的单选按钮不能用作选择器文本颜色。

Java代码

public class MainActivity extends AppCompatActivity {

    private RadioGroup radioGroup2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        radioGroup2=(RadioGroup)findViewById(R.id.rg2);



        for (int i = 0; i<5; i++) {
            RadioButton btn = (RadioButton) getLayoutInflater().inflate(R.layout.custom_radio, null);;
            btn.setText("dynamic "+ i);
            btn.setId(i);
            btn.setTag(i);
            btn.setTextColor(R.drawable.radio_text_selector_type1);
            radioGroup2.addView(btn);
        }
    }
}

XML 1

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

    <RadioGroup
        android:id="@+id/rg1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <RadioButton
            android:layout_width="match_parent"
            android:checked="true"
            android:layout_height="wrap_content"
            android:textColor="@drawable/radio_text_selector_type1"
            android:text="text 1"/>
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@drawable/radio_text_selector_type1"
            android:text="text 2"/>
        <RadioButton
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textColor="@drawable/radio_text_selector_type1"
            android:text="text 3"/>
    </RadioGroup>

    <RadioGroup
        android:id="@+id/rg2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </RadioGroup>
</LinearLayout>

XML 2

<?xml version="1.0" encoding="utf-8"?>
<RadioButton
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_product_variant_rb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="hello"
    android:padding="5dp"
    android:ems="7"
    android:textAllCaps="true"
    android:textColor="@drawable/radio_text_selector_type1"
    android:gravity="center"
    android:layout_margin="10dp">

</RadioButton>

选择器XML

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_checked="false"
        android:color="@android:color/darker_gray">
    </item>

    <item android:state_checked="true"
        android:color="@android:color/holo_green_dark">
    </item>
</selector>

结果:

如果您想通过 XML 为文本颜色选择器添加颜色,请使用此代码。

android:textColor="@color/radio_text_selector_type1"

但是,如果您想以编程方式设置选择器,请使用此代码 -

radioButton.setTextColor(ContextCompat.getColorStateList(getContext(), R.color.radio_text_selector_type1));

希望对您有所帮助