TextView的DrawableRight图片

来源:互联网 发布:ubuntu安装deb软件命令 编辑:程序博客网 时间:2024/05/21 07:02

1、在xml中为TextVIew添加Drawable


  <TextView            android:id="@+id/textview"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:background="@color/white"            android:drawableRight="@drawable/myicon"            android:padding="10dp"            android:text="文本内容"            android:textColor="@color/black"            android:textSize="20dp">        </TextView>



2、在代码中设置Drawable

setCompoundDrawables(Drawable left,Drawable top,Drawable right,Drawable bottom)


Drawable可以通过 Drawable drawable=getResources().getDrawable(R.drawable.myicon);得到
但是API提示,setCompoundDrawables() 调用的时候,Drawable对象必须调用setBounds(int left, int top, int right, int bottom)方法


 Drawable drawable = getResources().getDrawable(R.drawable.myicon); drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); textview.setCompoundDrawables(null, null, drawable, null);


0 0