android 设置TextView的文本 上下左右 图标

android屌丝估计都知道,TextView是个文本控件,但是有的android新手可能还不知道TextVeiw还可以给文本的上下左右设置图标,下面我们就来说说怎么给TexteView设置图标

给TextView设置图标有两种方法:一种是在xml布局文件中设置,一种是通过java代码设置.方法如下:

 1. xml布局文件中设置:

 

android:drawableLeft="@mipmap/ic_launcher"
android:drawableTop="@mipmap/ic_launcher"
android:drawableRight="@mipmap/ic_launcher"
android:drawableBottom="@mipmap/ic_launcher"

2.java代码中设置:

	TextView mTvMainTitleLeft = (TextView) findViewById(R.id.tv_title_left);
//      Drawable dwLeft = ContextCompat.getDrawable(getContext(), res); android studio中的获取方法
        Drawable dwLeft = getResources().getDrawable(R.mipmap.ic_launcher);
        dwLeft.setBounds(0, 0, dwLeft.getMinimumWidth(), dwLeft.getMinimumHeight());
        mTvMainTitleLeft.setCompoundDrawables(dwLeft, null, null, null);

以上就是给TextView设置图标的2中方式。

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值