TextView代码控制点击前一张图,点击后是另一张图

来源:互联网 发布:小米wifi软件下载 编辑:程序博客网 时间:2024/05/31 19:39

安卓在代码中设置TextView的drawableLeft、drawableRight、drawableTop、drawableBottom

<TextView            android:id="@+id/tv_good_point"            android:drawableTop="@drawable/good_point_false"            android:gravity="center"            android:layout_width="0dp"            android:layout_height="match_parent"            android:layout_weight="1"            android:text="点赞数" />

这里我设置的drawableTop,效果如下图显示

这里写图片描述

这时,我们要实现点击选框区域后实现改变图片的效果,在代码中如果要修改drawableRight设置的图片可以使用API

void android.widget.TextView.setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)

Drawable可以通过 Drawable rightDrawable = getResources().getDrawable(R.drawable.icon_new); 得到。
但是API提示,setCompoundDrawables() 调用的时候,Drawable对象必须调用setBounds(int left, int top, int right, int bottom)方法,于是我们加一行代码就可以了。

topDrawable_good_point.setBounds(0,0,topDrawable_good_point.getMinimumWidth(),topDrawable_good_point.getMinimumHeight());

整体代码如下:

 Drawable topDrawable_good_point = view.getResources().getDrawable(R.drawable.good_point_true);                if (topDrawable_good_point != null) {                    topDrawable_good_point.setBounds(0,0,topDrawable_good_point.getMinimumWidth(),topDrawable_good_point.getMinimumHeight());                }                tv_good_point.setCompoundDrawables(null,topDrawable_good_point,null,null);                //改变什么方向的图片就改变哪个Drawable
阅读全文
0 0
原创粉丝点击