Android 带图标的textview

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

Android 带图标的textview_第1张图片

在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. Android所有系统资源图标android.R.drawable查看
  2. Android有效解决加载大图片时内存溢出的问题
  3. RelativeLayout里常用的位置属性
  4. Android 图片加载图片_OOM异常解决
  5. 【Android 界面效果14】RelativeLayout里常用的位置属性
  6. android实现服务器图片本地缓存
  7. Android 下控件位置大小调整
  8. Android SeekBar自定义使用图片和颜色显示
  9. RecyclerView item imageview 图片宽高自适应

随机推荐

  1. Android UI开发第八篇——ViewFlipper 左
  2. Android模拟器环境中安装和删除应用程序
  3. Android设计模式-责任链
  4. Android技术栈(四)Android(安卓)Jetpack
  5. Android NDK: Host 'awk' tool is outdat
  6. 关于Android连接远程数据库(mysql、oracle
  7. Android(安卓)5.0 documentation CHM 版
  8. 整理分享ImageView属性大全
  9. android 休眠唤醒机制分析(一)
  10. 如何在横屏控制软键盘显示一部分