解决TextView drawableLeft左侧图片大小不可控的问题

设置完代码后图片的大小无效果

Drawable rightDrawable= context.getResources().getDrawable(R.drawable.more);
rightDrawable.setBounds(0, 0, 5, 5)
tvMore.setCompoundDrawablesWithIntrinsicBounds(null,null,rightDrawable,null);

解决方法

自定义ImageTextView控件


public class ImageTextView extends TextView {
    private Drawable mDrawable;//设置的图片
    private int mScaleWidth; // 图片的宽度
    private int mScaleHeight;// 图片的高度
    private int mPosition;// 图片的位置 

    public ImageTextView(Context context) {
        super(context);
    }

    public ImageTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ImageTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    public void init(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.ImageTextView);

        mDrawable = typedArray.getDrawable(R.styleable.ImageTextView_drawable);
        mScaleWidth = typedArray
                .getDimensionPixelOffset(
                        R.styleable.ImageTextView_drawableWidth,
                        DensityUtil.dip2px(20));
        mScaleHeight = typedArray.getDimensionPixelOffset(
                R.styleable.ImageTextView_drawableHeight,
                DensityUtil.dip2px(20));
        mPosition = typedArray.getInt(R.styleable.ImageTextView_position, 3);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mDrawable != null) {
            mDrawable.setBounds(0, 0, DensityUtil.dip2px(mScaleWidth),
                    DensityUtil.dip2px(mScaleHeight));

        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        switch (mPosition) {
        case 1:
            this.setCompoundDrawables(mDrawable, null, null, null);
            break;
        case 2:
            this.setCompoundDrawables(null, mDrawable, null, null);
            break;
        case 3:
            this.setCompoundDrawables(null, null, mDrawable, null);
            break;
        case 4:
            this.setCompoundDrawables(null, null, null, mDrawable);
            break;
        default:
            break;
        }
    }

    /**
     * 设置左侧图片并重绘
     * 
     * @param drawableLeft
     */
    public void setDrawableLeft(Drawable drawable) {
        this.mDrawable = drawable;
        invalidate();
    }

    /**
     * 设置左侧图片并重绘
     * 
     * @param drawableLeftRes
     */
    public void setDrawableLeft(int drawableRes, Context context) {
        this.mDrawable = context.getResources().getDrawable(drawableRes);
        invalidate();
    }
}

设置attrs.xml配置

   <declare-styleable name="ImageTextView">
       <attr name="drawable" format="reference"/>
       <attr name="drawableWidth" format="dimension"/>
       <attr name="drawableHeight" format="dimension"/>
       <attr name="position" format="integer"/>
   </declare-styleable>

布局文件中使用

 
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   xmlns:app="http://schemas.android.com/apk/res/com.xes.jazhanghui.activity"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="@drawable/bg_block_inset_white_ltr_8"
   android:orientation="vertical" >

  
       <com.xes.jazhanghui.views.ImageTextView
           android:id="@+id/tv_more"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:drawablePadding="5dp"
           android:text="更多"
           android:textColor="@color/color_737373"
           android:textSize="12sp"
           app:drawable="@drawable/more"
           app:drawableHeight="4dp"
           app:drawableWidth="3dp"
           app:position="3" />

   </RelativeLayout>

</LinearLayout>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,420评论 25 708
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,246评论 4 61
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,535评论 0 17
  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,894评论 22 665
  • 感赏自己今晚在女儿的督促下开始运动,感赏女儿的督促。 感赏自己在和女儿言语中因为学习上的事起冲突能控制住情绪,和以...
    dd100阅读 147评论 0 0