解决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 ...
随机推荐
- network is unreachable 网关PING不通解决办法
所里有几台机器没有办法ping通网关,但是ping交换机里的其它机器都可以ping通,其它机器ping网关也可以ping通.那么就排出了硬件的故障,主要问题就在问题机器的路由表上了. 看一下路由表 r ...
- 常见素数筛选方法原理和Python实现
1. 普通筛选(常用于求解单个素数问题) 自然数中,除了1和它本身以外不再有其他因数. import math def func_get_prime(n): func = lambda x: not ...
- Spring Boot 静态资源访问原理解析
一.前言 springboot配置静态资源方式是多种多样,接下来我会介绍其中几种方式,并解析一下其中的原理. 二.使用properties属性进行配置 应该说 spring.mvc.static-pa ...
- Elasticsearch 基于 URL 的搜索请求
背景 Elasticsearch 不像关系型数据库,没有简易的 SQL 用来查询数据,只能通过调用 RESTful API 实现查询.大体上查询分为两种,基于 URL 的和基于请求主体的.基于 URL ...
- 深度学习(TensorFlow)环境搭建:(三)Ubuntu16.04+CUDA8.0+cuDNN7+Anaconda4.4+Python3.6+TensorFlow1.3
紧接着上一篇的文章<深度学习(TensorFlow)环境搭建:(二)Ubuntu16.04+1080Ti显卡驱动>,这篇文章,主要讲解如何安装CUDA+CUDNN,不过前提是我们是已经把N ...
- PTA 5-3 解题报告
GitHub 问题描述 解题思路 代码 问题描述 5-3 计算平均值 (50分) 现在为若干组整数分别计算平均值. 已知这些整数的绝对值都小于100,每组整数的数量不少于1个,不大于20个. 输入格式 ...
- SQL varbinary varchar 互转
--============================================== -- FUNCTION varbin2hexstr -- 将 varbinary 类型的数据转换为 v ...
- 纯js上传文件 很好用
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- 关于linux kernel slab内存管理的一点思考
linux kernel 内存管理是个很大的话题,这里记录一点个人关于slab模块的一点思考总结. 有些书把slab介绍成高速缓存,这会让人和cache,特别是cpu cache混淆,造成误解.sla ...
- python系统编程(一)
进程的创建-fork 1. 进程 VS 程序 编写完毕的代码,在没有运行的时候,称之为程序 正在运行着的代码,就成为进程 进程,除了包含代码以外,还有需要运行的环境等,所以和程序是有区别的 2. fo ...