• 使用代码为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) 
  • 相关阅读:
    12.18-java复习-UserBean
    12.17-javaweb复习
    12.16-javaweb复习
    Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
    java建议
    个人表现
    今日总结
    今日总结
    今日总结
    今日总结
  • 原文地址:https://www.cnblogs.com/bianmajiang/p/4018717.html
Copyright © 2020-2023  润新知