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) 
  • 相关阅读:
    SnagIt 9-12 注册码
    【工具推荐】LICEcap –GIF 屏幕录制工具
    linux笔记一(基础命令)
    C#性能优化:延迟初始化Lazy<T>
    CSS3实现漂亮ToolTips
    mysql数据库sql优化
    精简代码,为网站减负的十大建议
    10个简单步骤,完全理解SQL
    13个mysql数据库的实用SQL小技巧
    MyBatis源码解读(二)
  • 原文地址:https://www.cnblogs.com/bianmajiang/p/4018717.html
Copyright © 2011-2022 走看看