您的位置:首页 > 移动开发 > Android开发

Android 带图标的textview

2019-08-05 17:33 2186 查看
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 本文链接:https://blog.csdn.net/u014714188/article/details/98502044

Android 带图标的textview

首先,我们看看最后的实现效果

在layout的实现

android:drawableTop 图标在文字的上方

<TextView
android:id="@+id/listmessage_top_price"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:drawablePadding="5dp"
android:drawableTop="@drawable/listmessage_top_price"
android:gravity="center_horizontal"
android:text="@string/listmessage_top_price" />

动态修改:

Drawable top = getResources().getDrawable(R.drawable.image);
button.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);

动态的获取drawable资源,然后设置给button或者 TextView:
button.setCompoundDrawablesWithIntrinsicBounds(null, top , null, null);
这个方法的四个参数分别是指,left ,top,right ,bottom。 也就是你要添加的 drawable图片相对于text的位置。如果不想在某个位置添加图片则设置为null即可。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: