zoukankan      html  css  js  c++  java
  • 使用代码为TextView设置drawableLeft

    在Android开发中有时候需要将ImageView和TextView作为一个整体显示出来,并能动态更改ImageView的图片和TextView的文字。此时一般有两种做法。

    方法一:自定义组合控件:

    public class CustomImageText extends LinearLayout {  
        private ImageView iv;  
        private TextView  tv;  
      
        public CustomImageText(Context context) {  
            this(context, null);  
        }  
      
        public CustomImageText(Context context, AttributeSet attrs) {  
            super(context, attrs);  
            // 导入布局  
            LayoutInflater.from(context).inflate(R.layout.imagetext, this, true);  
            iv = (ImageView) findViewById(R.id.iv); 
            tv = (TextView) findViewById(R.id.tv);  
        }  
      
        /** 
         * 设置图片资源 
         */  
        public void setImageResource(int resId) {  
            iv.setImageResource(resId);  
        }  
      
        /** 
         * 设置显示的文字 
         */  
        public void setText(String text) {  
            tv.setText(text);  
        }  
    }  

    然后在布局文件中引用全路径名。

    方法二:使用TextView的drawableLeft属性,图片和文字之间的间距使用drawablePadding调整。

    Drawable drawable= getResources().getDrawable(R.drawable.left);  
    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());  
    tv.setCompoundDrawables(drawable,null,null,null);  
    或者:
    public void setCompoundDrawablesWithIntrinsicBounds (Drawable left,  
    Drawable top, Drawable right, Drawable bottom) 
  • 相关阅读:
    .gitignore 文件没起作用
    HTML 中img标签不显示
    关于拖拽
    关于javascript三目
    封装ajax
    javascript-时间戳
    关于Vue实例的生命周期created和mounted的区别
    ES6核心内容讲解
    jsonp跨域请求
    javascript-AJAX
  • 原文地址:https://www.cnblogs.com/bianmajiang/p/4018717.html
Copyright © 2011-2022 走看看