TextView控件设置drawable

TextView控件设置drawable


  为TextView控件设置drawable有两种方式:

  1.在布局文件中通过drawableLeft、drawableTop、drawableRight或者drawableBottom属性来设置TextView任意一侧的图片,并可通过android:drawablePadding属性来设置文字与图片的间距;

  2.在java代码中通过TextView中的setCompoundDrawables()方法来动态设置图片

    Drawable drawable = ContextCompat.getDrawable(getContext(), R.mipmap.xxx);
    drawable.setBounds(left, top, right, bottom);//设置drawable显示在(left, top) 和(right, bottom)构成的矩形中
    drawable.setCompoundDrawablePadding(DensityUtils.dip2px(getContext(), 7));//相当于xml中的android:drawablePadding属性
    textView.setCompoundDrawables(null, null, drawable, null);//将drawable放置在textview的右侧;

猜你喜欢

转载自www.cnblogs.com/Ricardoldc/p/11772408.html
今日推荐