android 动态设置drawableTop,drawableLeft,drawableRight,drawableBottom.

android 动态设置drawableTop。


通常情况下我们都是在布局文件中设置drawableTop,drawableLeft,drawableRight,drawableBottom.




<TextView 
        android:id="@+id/title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:drawableTop="@drawable/height_icon"
        android:text="@string/height"
        android:layout_marginTop="20dp"
        android:textSize="30sp"
        android:textColor="@color/yellow"
        />

但是我们也会用带在代码当中动态设置的情况:
TextView mTitle = (TextView) view.findViewById(R.id.title);
Drawable top = getActivity().getResources().getDrawable(R.drawable.weight_icon);// 获取res下的图片drawable
top.setBounds(0, 0, top.getMinimumWidth(), top.getMinimumHeight());// 一定要设置setBounds();
// 调用setCompoundDrawables(Drawable left, Drawable top,Drawable right, Drawable bottom)方法。(有四个参数,不需要设置的参数传null)
mTitle.setCompoundDrawables(null, top, null, null);

版权声明:本文为qq_18481971原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。