Android 带图标的textview

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

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android Studio本身并不包含自图标库。然而,你可以通过使用第三方库或者自定义方法来实现在Android Studio中使用图标的功能。一个常见的方法是使用Material Design Icon Generator插件,你可以在Android Studio中通过以下步骤安装该插件: 1. 打开Android Studio,点击菜单栏中的"File",选择"Settings"。 2. 在弹出的窗口中,展开"Plugins",点击"Marketplace"。 3. 在搜索框中输入"Material Design Icon Generator",点击搜索结果中的插件。 4. 点击"Install"按钮进行安装。 5. 安装完成后,重启Android Studio。 安装完成后,你可以使用该插件来浏览和选择各种Material Design风格的图标,并将它们应用到你的项目中。 另外,如果你想自定义TextView并使用自定义字体(包括图标字体),你可以创建一个名为FontIconView的自定义TextView。在该类中,你可以通过设置字体来实现在xml中直接使用自定义字体,无需在Activity中手动设置Typeface。具体步骤如下: 1. 创建一个名为FontIconView的Java类,并继承自TextView。 2. 在该类的构造函数中调用一个init()方法,并在该方法中设置字体图标。 3. 通过在xml布局中使用FontIconView,来直接使用自定义字体。 此外,为了使用自定义字体文件,你需要在assets目录下创建一个名为"iconfont"的文件夹,并将字体文件"iconfont.ttf"复制到该文件夹中。 总结来说,Android Studio本身没有自图标库,但你可以通过安装插件或者自定义方法来实现在Android Studio中使用图标的功能。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Android下载自开源图标库教程](https://blog.csdn.net/m0_45281759/article/details/123503428)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [Android Studio使用IconFont阿里矢量图标库](https://blog.csdn.net/weixin_44177244/article/details/119773330)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值