TextView 上下左右设置图片的两种方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qulonglong110/article/details/84990373
1,text动态设置图片方法
 drawable = getResources().getDrawable(R.mipmap.an_icon_2x);
/// 这一步必须要做,否则不会显示.
 drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight())
//设置text 左边图片
text.setCompoundDrawables(drawable, null, null, null); //括号里面的四个参数代表左上右下四个方向的图片,可以同时设置多个

//****注意如果不想动态设置图片,也可以在布局里面设置

//2布局设置上面的图片
<TextView
    android:id="@+id/home_expert_btn"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:drawableTop="@mipmap/home_2x"
    android:drawablePadding="8dp"
    android:gravity="center"
    android:text="家"
    android:textColor="@color/c505050"
    android:textSize="@dimen/F14" />
android:drawablePadding="8dp"这一句是设置文字和图片的距离的,可以设置也可以不设置,如果图片和文字距离不理想可以设置这个参数

猜你喜欢

转载自blog.csdn.net/qulonglong110/article/details/84990373