解决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 ...
随机推荐
- python面试题之python下多线程的限制
python多线程有个全局解释器锁(global interpreter lock). 这个锁的意思是任一时间只能有一个线程使用解释器,跟单cpu跑多个程序一个意思,大家都是轮着用的,这叫“并发”,不 ...
- Spark中groupByKey、reduceByKey与sortByKey
groupByKey把相同的key的数据分组到一个集合序列当中: [("hello",1), ("world",1), ("hello",1 ...
- sql 的一些总结
如果用到“每” 就要用到group by 例:每个部门有多少人,就要用到分组技术 聚合函数一般作用在多条记录上 having 是分组厚的筛选条件,分组厚的数据组内再筛选,where 则是在分组 ...
- spring boot默认访问静态资源
演示spring boot默认可以直接访问静态资源的2种方法: 第一种:在src/main/resources资源目录下创建一个名为"static"的文件夹(该文件夹的名称是规定死 ...
- 实现DataGridView控件中CheckBox列的使用
最近做WindowsForms程序,使用DataGridView控件时,加了一列做选择用,发现CheckBox不能选中.搜索后,要实现DataGridView的CellContentClick事件,将 ...
- C#中form窗体之间的传递几个方法
- Java并发编程(十二)-- 阻塞队列
在介绍Java的阻塞队列之前,我们简单介绍一下队列. 队列 队列是一种数据结构.它有两个基本操作:在队列尾部加人一个元素,和从队列头部移除一个元素就是说,队列以一种先进先出的方式管理数据,如果你试图向 ...
- Android:ViewGroup和View的Touch事件
Android中ViewGroup和View中的Touch事件传递机制分析 关键字:GroupView:View:Touch事件 基础知识: onInterceptTouchEvent():在View ...
- ubuntu16 64 搭建lnmp环境
//安全设置linux(ubuntu16 64) 安全设置1.修改ssh端口 vi /etc/ssh/sshd_config 如果用户想让22和60000端口同时开放,只需在/etc/ssh/sshd ...
- 潭州课堂25班:Ph201805201 django 项目 第十七课 用户登录,登出实现 (课堂笔记)
登录,校验: 1,判断用户名输入是否为空, 2,判断用户名密码是否匹配, 3,记住我的功能,:将用户信息记到 session 中 请求方式: POST 在视图中: # 1,创建类# 2,获取前台参数# ...