Making RadioGroup radio buttons with images in Android… How?

社会主义新天地 提交于 2019-11-27 14:37:26

问题


Is it possible to make Radio Buttons in Android, where there are image representation to a button instead of a regular buttons? I want it to look something like that:

The cyan in the middle represents the selected button.

How can I do it?


回答1:


The RadioButton class has a buttonDrawable member, derived from CompoundButton.

There are multiple ways you can change this drawable.

From java:

myRadioButton.setButtonDrawable(resourceId or Drawable);

From xml:

<RadioButton
    android:button="@drawable/yourdrawable" 
    ...
/>

If you just want the checked/unchecked state look differently, then

  • Add a new selector xml by rightclicking your res folder -> New -> Android xml -> select Drawable in the upper dropdown -> choose selector (in eclipse)
  • To setup this selector, please take a look at this link
  • From now on you can set your freshly created selector to any of your checkboxes/radiobuttons by using its id.


来源:https://stackoverflow.com/questions/11853245/making-radiogroup-radio-buttons-with-images-in-android-how

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