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

时间:2018-01-25 07:26:46

标签: java android radio-button radio-group

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

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>

结果: enter image description here

1 个答案:

答案 0 :(得分:1)

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

android:textColor="@color/radio_text_selector_type1"

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

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

希望有所帮助