问题
I am using a ToggleButton
with an image. The image is 16x16, however, I want the button to be 50x50 so that when users click around the image even then the action is taken. This is how I'm avoiding the fat finger problem.
This is how my ToggleButton
is in the XML
<ToggleButton android:id="@+id/fav"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#FFFFFF"
android:gravity="center"
android:focusable="false"
android:layout_marginRight="10dp"
android:padding="15dp"
/>
and I set the background programmatically like this:
if (holder.favButton.isChecked())
holder.favButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.star_2));
else
holder.favButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.star_1));
Problem
The image looks expanded and blurry because the width and height is 50dp
, however, the image is only 16x16. Instead of setting the background with setBackgroundDrawable
, I want to find a way to set the image src
of the button. Because if I'm setting the background then the image takes over the entire background which is 50x50.
回答1:
Can this help you? State List
I alway using a custom drawable with Selector
and set it to ToggleButton
来源:https://stackoverflow.com/questions/24021032/is-there-a-way-to-programmatically-set-src-image-for-togglebutton