解决TextView drawableRight左侧图片大小不可控的问题
通过代码来修改图片的大小:
Drawable rightDrawable= context.getResources().getDrawable(R.drawable.more);
rightDrawable.setBounds(0, 0, drawable.getIntrinsicWidth() / 2, drawable.getIntrinsicHeight() / 2)
tvMore.setCompoundDrawablesWithIntrinsicBounds(null,null,rightDrawable,null);
其他解决方法
自定义ImageTextView控件
/**
* Package Name:com.xes.jazhanghui.views ClassName: ImageTextView <br/>
* Description: 解决TextView 图片大小不可控问题. <br/>
* Date: 2017-2-21 下午4:52:45 <br/>
*
* @author lihuixin
* @version
*/public class ImageTextView extends TextView {
private Drawable mDrawable;//设置的图片private int mScaleWidth; // 图片的宽度private int mScaleHeight;// 图片的高度private int mPosition;// 图片的位置 1上2左3下4右public ImageTextView(Context context) {
super(context);
}
public ImageTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
public ImageTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs);
}
public void init(Context context, AttributeSet attrs) {
TypedArray typedArray = context.obtainStyledAttributes(attrs,
R.styleable.ImageTextView);
mDrawable = typedArray.getDrawable(R.styleable.ImageTextView_drawable);
mScaleWidth = typedArray
.getDimensionPixelOffset(
R.styleable.ImageTextView_drawableWidth,
DensityUtil.dip2px(20));
mScaleHeight = typedArray.getDimensionPixelOffset(
R.styleable.ImageTextView_drawableHeight,
DensityUtil.dip2px(20));
mPosition = typedArray.getInt(R.styleable.ImageTextView_position, 3);
}
@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mDrawable != null) {
mDrawable.setBounds(0, 0, DensityUtil.dip2px(mScaleWidth),
DensityUtil.dip2px(mScaleHeight));
}
}
@Overrideprotected void onDraw(Canvas canvas) {
super.onDraw(canvas);
switch (mPosition) {
case 1:
this.setCompoundDrawables(mDrawable, null, null, null);
break;
case 2:
this.setCompoundDrawables(null, mDrawable, null, null);
break;
case 3:
this.setCompoundDrawables(null, null, mDrawable, null);
break;
case 4:
this.setCompoundDrawables(null, null, null, mDrawable);
break;
default:
break;
}
}
/**
* 设置左侧图片并重绘
*
* @param drawableLeft
*/public void setDrawableLeft(Drawable drawable) {
this.mDrawable = drawable;
invalidate();
}
/**
* 设置左侧图片并重绘
*
* @param drawableLeftRes
*/public void setDrawableLeft(int drawableRes, Context context) {
this.mDrawable = context.getResources().getDrawable(drawableRes);
invalidate();
}
}
设置attrs.xml配置
<declare-styleable name="ImageTextView">
<attr name="drawable" format="reference"/>
<attr name="drawableWidth" format="dimension"/>
<attr name="drawableHeight" format="dimension"/>
<attr name="position" format="integer"/>
</declare-styleable>
布局文件中使用
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res/com.xes.jazhanghui.activity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/bg_block_inset_white_ltr_8"
android:orientation="vertical" >
<com.xes.jazhanghui.views.ImageTextView
android:id="@+id/tv_more"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="5dp"
android:text="更多"
android:textColor="@color/color_737373"
android:textSize="12sp"
app:drawable="@drawable/more"
app:drawableHeight="4dp"
app:drawableWidth="3dp"
app:position="3" />
</RelativeLayout>
</LinearLayout>
解决TextView drawableRight左侧图片大小不可控的问题的更多相关文章
- 解决CIFilter滤镜后图片大小和方向发生变化
调用contextWithOptions:和createCGImage: fromRect:方法创建CIContext.与以往不同的地方是CIImage没有frame与bounds属性:只有exten ...
- 利用VBA宏批量解决Word中图片大小、居中设置
需求:经常阅读网上的研报(没钱买排版漂亮的高质量研报),有些需要保存的复制下来到word里,图片很大都超出word的边界了,也没有居中,手工一张张调整不现实,上百页的研报,几十张图片. 解决方案:利用 ...
- 3.Android 优化布局(解决TextView布局)
转载:http://www.jianshu.com/p/d3027acf475a 今天分享一个Layout布局中的一个小技巧,希望看过之后你也可以写出性能更好的布局,我个人的目的是用最少的view写出 ...
- WebView加载HTML图片大小自适应与文章自动换行
http://www.brighttj.com/ios/ios-webview-load-html-image-adaptive.html 在很多App中都会使用到webview,尤其是在加载新闻内容 ...
- 图片_ _Android有效解决加载大图片时内存溢出的问题 2
Android有效解决加载大图片时内存溢出的问题 博客分类: Android Android游戏虚拟机算法JNI 尽量不要使用setImageBitmap或 setImageResource或 Bit ...
- ImageView设置点击效果没有用?ImageView src的图片大小改变不了?
ImageView设置点击效果没有用? 解决 1.ImageView xml里面必须clickable 和longClickable为true <ImageView android:layout ...
- Android开发中如何解决加载大图片时内存溢出的问题
Android开发中如何解决加载大图片时内存溢出的问题 在Android开发过程中,我们经常会遇到加载的图片过大导致内存溢出的问题,其实类似这样的问题已经屡见不鲜了,下面将一些好的解决方案分享给 ...
- 我的Android进阶之旅------> Android在TextView中显示图片方法
面试题:请说出Android SDK支持哪些方式显示富文本信息(不同颜色.大小.并包括图像的文本信息).并简要说明实现方法. 答案:Android SDK支持例如以下显示富文本信息的方式. 1.使用T ...
- mui-图文列表 图片大小问题
下面是源码,不能调节图片大小 <ul class="mui-table-view"> <li class="mui-table-view-cell mu ...
随机推荐
- 2018牛客网暑假ACM多校训练赛(第七场)I Tree Subset Diameter 动态规划 长链剖分 线段树
原文链接https://www.cnblogs.com/zhouzhendong/p/NowCoder-2018-Summer-Round7-I.html 题目传送门 - https://www.n ...
- String.getBytes()和String.tocharArray(),字节数组和字符数组的区别
String.getBytes()是将字符串转化为一个字节数组.而String.toCharArray()是将一个字符串转化为一个字符数组. [例如] byte bys[] ="国庆60周年 ...
- P1182 数列分段`Section II` P1316 丢瓶盖 二分答案
题目描述 对于给定的一个长度为N的正整数数列A-iA−i,现要将其分成M(M≤N)M(M≤N)段,并要求每段连续,且每段和的最大值最小. 关于最大值最小: 例如一数列4 2 4 5 142451要分成 ...
- Linux系统数据共享-NFS服务
转载:http://www.cnblogs.com/mchina/archive/2013/01/03/2840040.html 一.NFS服务简介 NFS 是Network File System的 ...
- TF之BN:BN算法对多层中的每层神经网络加快学习QuadraticFunction_InputData+Histogram+BN的Error_curve
# 23 Batch Normalization import numpy as np import tensorflow as tf import matplotlib.pyplot as plt ...
- Python虚拟环境的安装和配置-virtualenv与windows下多个python版本共存
Python虚拟环境的安装和配置-virtualenv与windows下多个python版本共存 windows下多个python版本共存 https://www.python.org/downloa ...
- Django之模板基础
Django之模板 目录 变量 过滤器 标签的使用 变量 变量的引用格式 使用双括号,两边空格不能省略. 语法格式: {{var_name}} Template和Context对象 context 字 ...
- 满血复活--来自世一大的WAR
最需要复习的清单 1.二分 2.图论 3.数论 4.dp
- js 解析json
一 ,eval var dataObj=eval("("+data+")"); 1,这种形式将使得性能显著降低,因为它必须运行编译器 2,eval函数还减弱了你 ...
- Redis自学笔记:3.4入门-列表类型
3.4列表类型 3.4.1介绍 列表类型可以存储一个有序的字符串列表,常用的操作是向列表两端添加元素,或者 获得列表的某一片段. 优点:内部使用的是双向链表,所以向列表两端添加元素的时间复杂度为O(1 ...