我有一个既有文字又有图像的单选按钮。我试图通过在其上方应用颜色(如色调)来更改单击按钮时图像的颜色。我尝试使用android:state_checked="true"
,但它不允许我在图像上方应用颜色。
我还试图以编程方式执行:
RadioButton radioButtonShare= (RadioButton) findViewById(R.id.image_share);
radioButtonShare.getButtonDrawable().setColorFilter(getResources().getColor(R.color.colorPrimaryDark), PorterDuff.Mode.SRC_ATOP);
但它与nullpointexception崩溃了......
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setColorFilter(int, android.graphics.PorterDuff$Mode)' on a null object reference
请帮助
单选按钮
<RadioButton
android:id="@+id/image_share"
style="@style/style_radiobutton"
android:layout_height="match_parent"
android:drawableTop="@drawable/selector_image_share"
android:text="@string/edit_share" />
@绘制/ selector_image_share
<item android:drawable="@drawable/viewer_ic_share" android:state_checked="true" />
<item android:drawable="@drawable/viewer_ic_share" android:state_pressed="true" />
<item android:drawable="@drawable/viewer_ic_share" />
答案 0 :(得分:0)
我认为以下代码可以帮助您:
在style.xml文件中粘贴下面的代码
style.xml
<style name="radionbutton"
parent="Base.Widget.AppCompat.CompoundButton.RadioButton">
<item name="android:button">@android:color/transparent</item>
<item name="android:drawableTop">@drawable/radiobutton_drawable</item>
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowContentOverlay">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">true</item>
</style>
在drawable文件夹中创建radiobutton_drawable.xml文件,并在radiobutton_drawable.xml文件中粘贴以下代码。
radiobutton_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/radio_uncheck" android:state_checked="false" android:state_focused="true"/>
<item android:drawable="@drawable/radio_uncheck" android:state_checked="false" android:state_focused="false"/>
<item android:drawable="@drawable/radio_check" android:state_checked="true" android:state_focused="true"/>
<item android:drawable="@drawable/radio_check" android:state_checked="true" android:state_focused="false"/>
</selector>
粘贴在activity_main.xml文件中的代码下面。
<强> activity_main.xml中强>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/radio2"
style="@style/radionbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="male" />
<RadioButton
style="@style/radionbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@+id/radio2`enter code here`"
android:text="female" />
</RadioGroup>
</RelativeLayout>