有没有一种方法以编程方式设置SRC图像切换按钮? [英] Is there a way to programmatically set src image for ToggleButton?

查看:233
本文介绍了有没有一种方法以编程方式设置SRC图像切换按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是切换按钮用图像。该图像是16×16,但是,我希望按钮被50X50,这样,当用户点击即使再采取行动在图像周围。这就是我如何避免胖手指问题。

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.

这是怎么我的切换按钮是XML

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));

问题

图片看起来扩大和模糊,因为宽度和高度 50dp ,但是,图像仅16×16。而不是设置与 setBackgroundDrawable 的背景下,我想找到一种方法来设置按钮的图像的src 。因为如果我设置背景图像将占据整个背景是50×50。

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.

推荐答案

这能帮助你吗? 国家列表
我送花儿给人使用自定义绘制与选择键,将其设置为切换按钮

Can this help you? State List I alway using a custom drawable with Selector and set it to ToggleButton

这篇关于有没有一种方法以编程方式设置SRC图像切换按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆