Is there a way to programmatically set src image for ToggleButton?

佐手、 提交于 2019-12-11 00:36:23

问题


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

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