zoukankan      html  css  js  c++  java
  • 解决TextView drawableRight左侧图片大小不可控的问题

    通过代码来修改图片的大小:

    Drawable rightDrawable= context.getResources().getDrawable(R.drawable.more);
    rightDrawable.setBounds(0, 0, drawable.getIntrinsicWidth() / 2, drawable.getIntrinsicHeight() / 2)
    tvMore.setCompoundDrawablesWithIntrinsicBounds(null,null,rightDrawable,null);

    其他解决方法

    自定义ImageTextView控件

    /**
     * Package Name:com.xes.jazhanghui.views ClassName: ImageTextView <br/>
     * Description: 解决TextView 图片大小不可控问题. <br/>
     * Date: 2017-2-21 下午4:52:45 <br/>
     * 
     * @author lihuixin
     * @version
     */public class ImageTextView extends TextView {
        private Drawable mDrawable;//设置的图片private int mScaleWidth; // 图片的宽度private int mScaleHeight;// 图片的高度private int mPosition;// 图片的位置 1上2左3下4右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);
        }
    
        @Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            if (mDrawable != null) {
                mDrawable.setBounds(0, 0, DensityUtil.dip2px(mScaleWidth),
                        DensityUtil.dip2px(mScaleHeight));
    
            }
        }
    
        @Overrideprotected 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>





  • 相关阅读:
    2018-09-13 代码翻译尝试-使用Roaster解析和生成Java源码
    2018-09-10 使用现有在线翻译服务进行代码翻译的体验
    2018-09-06 Java实现英汉词典API初版发布在Maven
    2018-08-29 浏览器插件实现GitHub代码翻译原型演示
    2018-08-27 使用JDT核心库解析JDK源码后初步分析API命名
    2018-08-11 中文代码示例之Spring Boot 2.0.3问好
    2018-08-24 中文代码之Spring Boot对H2数据库简单查询
    2018-08-22 为中文API的简繁转换库添加迟到的持续集成
    2018-08-21 中文关键词替换体验页面原型
    vim打开不同的文件
  • 原文地址:https://www.cnblogs.com/jeffen/p/6899198.html
Copyright © 2011-2022 走看看