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) 
  • 相关阅读:
    STM32cubemx-HAL库串口断线问题
    stm32 微秒定延时问题
    JLink OB SWI 取代串口打印的方式
    英特尔神经棒使用入门-NCS2 & NCS1 -OpenVino
    计算机组成原理-第4章-4.1
    计算机组成原理-第3章-3.5
    计算机组成原理-第3章-3.4
    计算机组成原理-第3章-3.3
    Tensorflow Chapter-6
    计算机组成原理-第3章-3.2
  • 原文地址:https://www.cnblogs.com/bianmajiang/p/4018717.html
Copyright © 2011-2022 走看看