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即可。

更多相关文章

  1. 关于RelativeLayout的一点经验
  2. 点击LinearLayout使用selector改变TextView字体颜色
  3. 改变android程序head样式
  4. Android所有系统资源图标android.R.drawable查看
  5. RelativeLayout里常用的位置属性
  6. android 底部Tab添加水波纹效果
  7. 二、 Android中gravity与layout_gravity的区别
  8. 【Android(安卓)界面效果14】RelativeLayout里常用的位置属性
  9. Android开发者的Air For Android简单入门

随机推荐

  1. android SDK包引用了java SDK哪些包
  2. Android中使用handler
  3. 服务器端返回给客户端的数据格式
  4. Android内核驱动——电源管理
  5. Android(安卓)Animation学习笔记
  6. android 开发 文件读写应用案例分析
  7. Android(安卓)APN设置 获取APN列表
  8. Android(安卓)Animation学习笔记
  9. ANDROID开机动画bootanimation.zip的详细
  10. fir.im Weekly - 8 个不能错过的 iOS / A