Android自定义ViewAndroid自定义控件

Android 自定义 View-如何设置TextView dr

2017-08-24  本文已影响300人  mrwangyong

项目中经常需要设置 drawable 大小,但是可惜 textView 并不支持

手摸手,三分钟动手造轮子:

支持 任意 Drawable,不限于图片, XML,layer,再也不怕产品的各种奇葩需求了

  1. 创建自定义的 View
public class DrawableTextView extends AppCompatTextView {
//设置方向
public static final int LEFT = 1, TOP = 2, RIGHT = 3, BOTTOM = 4;
  public DrawableTextView(Context context) {
    this(context, null);
  }

  public DrawableTextView(Context context, AttributeSet attrs){
    this(context, attrs, 0);
  }

  public DrawableTextView(Context context, AttributeSet attrs, 
    int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(attrs);
  }
}
  1. attrs.xml设置自定义属性:
<declare-styleable name="DrawableTextView">
    <attr name="drawable_src" format="reference"/>
    <attr name="drawable_height" format="dimension"/>
    <attr name="drawable_width" format="dimension"/>
    <attr name="drawable_location">
      <enum name="left" value="1"/>
      <enum name="top" value="2"/>
      <enum name="right" value="3"/>
      <enum name="bottom" value="4"/>
    </attr>
  </declare-styleable>
  1. 解析自定义属性
  private void init(AttributeSet attrs) {
    if (attrs != null) {
      TypedArray a = getContext().obtainStyledAttributes(attrs,             R.styleable.DrawableTextView);
      int mWidth = a.getDimensionPixelSize
        (R.styleable.DrawableTextView_drawable_width, 0);
      int mHeight =a.getDimensionPixelSize
        (R.styleable.DrawableTextView_drawable_height,0);
      Drawable mDrawable =a.getDrawable
        (R.styleable.DrawableTextView_drawable_src);
      int mLocation = a.getInt
        (R.styleable.DrawableTextView_drawable_location, LEFT);
      a.recycle();
      drawDrawable(mDrawable, mWidth, mHeight, mLocation);
    }
  }
  1. 给 Drawable 设置好 参数

    /**
       * 绘制Drawable宽高,位置
       */
      public void drawDrawable(Drawable mDrawable, int mWidth,           int mHeight, int mLocation) {
        if (mDrawable != null) {
          if (mWidth != 0 && mHeight != 0) {
            mDrawable.setBounds(0, 0, mWidth, mHeight);
          }
          switch (mLocation) {
            case LEFT:
              this.setCompoundDrawables(mDrawable, null,
                  null, null);
              break;
            case TOP:
              this.setCompoundDrawables(null, mDrawable,
                  null, null);
              break;
            case RIGHT:
              this.setCompoundDrawables(null, null,
                  mDrawable, null);
              break;
            case BOTTOM:
              this.setCompoundDrawables(null, null, null,
                  mDrawable);
              break;
          }
        }
      }
    
  2. 最后,使用:

    <com.mrwang.gifstudio.View.DrawableTextView
        android:id="@+id/view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="hahahah"
        app:drawable_src="@drawable/count_down"
        android:drawablePadding="10dp"
        app:drawable_height="50dp"
        app:drawable_width="50dp"
        app:drawable_location="right"/>
    

  3. 贴上最终效果图:

  1. 所有代码:

    public class DrawableTextView extends AppCompatTextView {
    
      public static final int LEFT = 1, TOP = 2, RIGHT = 3, BOTTOM = 4;
    
      public DrawableTextView(Context context) {
        this(context, null);
      }
    
      public DrawableTextView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
      }
    
      public DrawableTextView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(attrs);
      }
    
      private void init(AttributeSet attrs) {
        if (attrs != null) {
          TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.DrawableTextView);
          int mWidth = a
              .getDimensionPixelSize(R.styleable.DrawableTextView_drawable_width, 0);
          int mHeight = a.getDimensionPixelSize(R.styleable.DrawableTextView_drawable_height,
              0);
          Drawable mDrawable = a.getDrawable(R.styleable.DrawableTextView_drawable_src);
          int mLocation = a.getInt(R.styleable.DrawableTextView_drawable_location, LEFT);
          a.recycle();
          drawDrawable(mDrawable, mWidth, mHeight, mLocation);
        }
      }
    
      /**
       * 绘制Drawable宽高,位置
       */
      public void drawDrawable(Drawable mDrawable, int mWidth, int mHeight, int mLocation) {
        if (mDrawable != null) {
          if (mWidth != 0 && mHeight != 0) {
            mDrawable.setBounds(0, 0, mWidth, mHeight);
          }
          switch (mLocation) {
            case LEFT:
              this.setCompoundDrawables(mDrawable, null,
                  null, null);
              break;
            case TOP:
              this.setCompoundDrawables(null, mDrawable,
                  null, null);
              break;
            case RIGHT:
              this.setCompoundDrawables(null, null,
                  mDrawable, null);
              break;
            case BOTTOM:
              this.setCompoundDrawables(null, null, null,
                  mDrawable);
              break;
          }
        }
      }
    }
    

    ​参考:
    | http://blog.csdn.net/u014702653/article/details/52304656

上一篇 下一篇

猜你喜欢

热点阅读