Проблема с цветом текста переключателя при динамическом добавлении
У меня есть кнопка Radio внутри группы Radio, одна группа Radio создается программно, а другая из XML, Radio кнопка, которая создается 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>
1 ответ
Решение
Если вы хотите, чтобы текст цвета селектора через XML, пожалуйста, используйте этот код.
android:textColor="@color/radio_text_selector_type1"
Однако, если вы хотите установить селектор программно, используйте этот код -
radioButton.setTextColor(ContextCompat.getColorStateList(getContext(), R.color.radio_text_selector_type1));
Надеюсь, поможет