天天看点

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中方式。