解决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 ...
随机推荐
- HDU2449 Gauss Elimination 高斯消元 高精度 (C++ AC代码)
原文链接https://www.cnblogs.com/zhouzhendong/p/HDU2449.html 题目传送门 - HDU2449 题意 高精度高斯消元. 输入 $n$ 个 $n$ 元方程 ...
- 001_ajax学习
1,XMLHttpRequest 2,window.ActioveXObject(ie浏览器) 3,new ActiveXObject("Microsoft.XMLHTTP") 4 ...
- 给定两个字符串 s 和 t,它们只包含小写字母。 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母。 请找出在 t 中被添加的字母。
给定两个字符串 s 和 t,它们只包含小写字母.字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母.请找出在 t 中被添加的字母. 示例: 输入: s = "abcd" ...
- day24 面向对象,交互,组合,命名空间,初始继承
面向对象的命名空间: #属性:静态属性 (直接和类名关联或者直接定义在class下的变量) # 对象属性 (在类内和self关联,在类外和对象名关联的变量) # 动态属性(函数) class Foo: ...
- mybatis中if test 可以使用== != null '' and or 和括号()
<if test="pd.flag==1 or ((pd.flag==2 or pd.flag==3) and (pd.sfyj==2 or pd.sfyj==3)) or pd.fl ...
- Django的请求生命周期
Django的请求生命周期 请求生命周期 请求生命周期是指当用户在浏览器上输入url到用户看到网页的这个时间段内,Django后台所发生的事情. 1.客户端发送Http请求 2 .服务器接收,根据请求 ...
- JavaScript 作用域的误区
了解JavaScript的同学可能知道,JavaScript语言由于设计原因,导致语言本身存在很多先天性的不足,当然这并非设计者有意的,js语言最初是被设计来作为网页交互的脚本语言,依照现有的js语法 ...
- Summary
PDF 暑假开始准备转移博客,试了几个都不怎么满意(我还去试了下LineBlog 不知道那时候在想什么..),屯着一堆文章,,到时候一起发了 现在暂时转移至WordPress,不过还在完善中,预计.. ...
- Vuejs学习笔记(一)
1) vue.js 的 主要功能 1.模板渲染 2.模块化 3.扩展功能:路由,Ajax 2) vue.js 模板渲染之双向绑定示例 message 绑定到了 input 对象,所有在in ...
- BZOJ.5338.[TJOI2018]xor(可持久化Trie)
BZOJ LOJ 洛谷 惊了,18年了还有省选出模板题吗= = 做这题就是练模板的,我就知道我忘的差不多了 询问一就用以DFS序为前缀得到的可持久化Trie做,询问二很经典的树上差分. 注意求询问二的 ...