setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds区别

来源:互联网 发布:数字油画 知乎 编辑:程序博客网 时间:2024/05/22 00:06

http://blog.csdn.net/zwq1457/article/details/8088368

手工设置文本与图片相对位置时,常用到如下方法:
   setCompoundDrawables(left, top, right, bottom);
   setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)
   意思是设置Drawable显示在text的左、上、右、下位置。
  (Textview、Button都可以)
   但是两者有些区别:
   setCompoundDrawables 画的drawable的宽高是按drawable.setBound()设置的宽高,
   所以才有The Drawables must already have had setBounds(Rect) called.
   意思是说使用之前必须使用Drawable.setBounds设置Drawable的长宽。
   而setCompoundDrawablesWithIntrinsicBounds是画的drawable的宽高是按drawable固定的宽高,
   即通过getIntrinsicWidth()与getIntrinsicHeight()获得,所以才有

  The Drawables' bounds will be set to their intrinsic bounds.这句话之说!
 
[html] view plaincopyprint?
  1. private AutoCompleteTextView mEditText;  
  2.   
  3. mEditText = new AutoCompleteTextView(context,attrs);  
  4.   mEditText.setHint(R.string.btn_search);  
  5.   mEditText.setTextSize(14);  
  6.   mEditText.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.iphonesearch_search_3), null, null, null);  
  7.   mEditText.addTextChangedListener(new TextWatcher(){  
  8.    public void afterTextChanged(Editable s) {}  
  9.    public void beforeTextChanged(CharSequence s, int start, int count,int after) {}  
  10.    public void onTextChanged(CharSequence s, int start, int before,int count) {  
  11.     boolean isSearch = false;  
  12.     if (s == null  || s.toString().equals("")) {  
  13.      mClose.setVisibility(View.INVISIBLE);  
  14.      isSearch = false;  
  15.     } else {  
  16.      mClose.setVisibility(View.VISIBLE);  
  17.      isSearch = true;  
  18.     }  
  19.     mListener.startSearch(isSearch,s.toString());  
  20.    }});  
  21.   mEditText.setOnFocusChangeListener(new OnFocusChangeListener(){  
  22.    public void onFocusChange(View v, boolean hasFocus) {  
  23.     if (!firstFocusChange) {  
  24.      firstFocusChange = true;  
  25.      if (mListener != null) {  
  26.       mListener.getFirstFocus();  
  27.      }  
  28.      Animation ca =new ScaleAnimation(1.0f,0.8f,1.0f,1.0f);  
  29.      ca.setFillAfter(true);  
  30.      ca.setDuration(300);  
  31.      mBackground.startAnimation(ca);  
  32.     }  
  33.    }});  
  34.   mEditText.setBackgroundColor(0);  
  35.   mEditText.setMaxWidth(240);  
  36.   mEditText.setMinWidth(240);  
  37.   mEditText.setMaxLines(1);  
  38.   mEditText.setMinLines(1);  

0 0
原创粉丝点击