How to customize default theme of radio button in android?

做~自己de王妃 提交于 2020-01-15 05:03:13

问题


i want to change default theme of radio button to custom but it's not changing the theme. please help me out

styles.xml

<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="android:radioButtonStyle">@style/radionbutton</item>
</style>

<!-- custom style -->
<style name="radionbutton"
     parent="Base.Widget.AppCompat.CompoundButton.RadioButton">
    <item name="android:button">@drawable/radiobutton_drawable</item>
</style>

radiobutton_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:drawable="@mipmap/radio_unselected" >
</item>
<item android:state_checked="true" android:drawable="@mipmap/radio_selected">
</item>


回答1:


Declare custom style in your styles.xml file.

<style name="MyRadioButton" parent="Theme.AppCompat.Light">  
  <item name="colorControlNormal">@color/indigo</item>
  <item name="colorControlActivated">@color/pink</item>
</style>  

Apply this style to your RadioButton via android:theme attribute.

<RadioButton  
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="Radio Button"
    android:theme="@style/MyRadioButton"/>

only if your activity extends AppCompatActivity




回答2:


Finally, i found answer by myself

I just create custom layout and add to ListAdapter object

radiobuttonlayout.xml

<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/checkedTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checkMark="@drawable/radiobutton_drawable"
android:gravity="center_vertical"
android:text="New CheckedTextView"
android:padding="@dimen/dp10"
android:textColor="@color/white" />

Java file code

ListAdapter listAdapter = new ArrayAdapter<String>    (context,R.layout.radiobuttonlayout,aryStrLockscreens);
    listview_lockscreen.setAdapter(listAdapter);

It's worked for me.




回答3:


The radiobutton_drawable.xml has a missing </selector> tag in the end. Plus you should do this to your radio button-

<RadioButton android:layout_width="wrap_content"
           android:layout_marginTop="20dp"
           android:id="@+id/radio"
           style="@style/radionbutton"
           android:layout_height="wrap_content"/>



回答4:


Need to apply individual style to radio button

<RadioButton
        android:layout_width="wrap_content"
        style="@style/radiobutton_drawable"
        android:layout_height="wrap_content"/>


来源:https://stackoverflow.com/questions/35150890/how-to-customize-default-theme-of-radio-button-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!