看着类似这样的UI,如果我们用ImageView 和TextView 组合来设计,那就有点小题大做了,其实一个TextView就足够了。 xml中的TextView: <TextView android:id="@+id/tv_about" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:drawableLeft="@mipmap/ic_launcher" android:text="关于我们" />
我们在xml中设置了 DrawableLeft。 那要是我想在代码中改变drawable。有什么方法可以使用代码为TextView设置drawableLeft呢? 答案是有的。 Drawable drawable= ContextCompat.getDrawable(this, R.mipmap.ic_launcher); drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); textView.setCompoundDrawables(drawable,null,null,null);
这里要注意的是,需要设置setBounds,不然不会正常显示。 对此,我们还可以通过setCompoundDrawablesWithIntrinsicBounds这样设置. textView.setCompoundDrawablesWithIntrinsicBounds(drawable,null,null,null);
具体可以参考 public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top, Drawable right, Drawable bottom) { }
[END] 希望今天的文章对你有帮助,感谢阅读,感谢有你!
|