TextView 在xml 中设置图片大小
TextView xml中只能设置图片的位置,但是不能再xml中设置图片的大小
android:drawableStart="@drawable/pic"
在代码中可以啊对textview 的图片进行设置,主要代码如下:
//设置图片的上下左右的位置,也就是宽高
drawable.setBounds(left, top, right, bottom);
//设置textView上下左右的图片
textView.setCompoundDrawables(startDrawable,topDrawable,endDrawable,bottomDrawable);
这里介绍一个我根据网上的代码,修改的自定义的TextView ,可以在xml中设置上下左右图片的大小,并且支持从RTL 布局
效果展示
先来看看效果:
使用方法:
<com.xuexuan.view.TextImageViewandroid:id="@+id/tv_bank_card"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="text的内容"android:drawableStart="@drawable/pic"app:drawableStartHeight="20dp"app:drawableStartWidth="20dp"/>
其中android:drawableStart="@drawable/pic"
是设置左边显示图片,app:drawableStartHeight="20dp"
和 app:drawableStartWidth="20dp"
是设置图片的宽高,注意一定要使用Start、End 而不是Left,Right
源码
资源属性
在attrs.xml中添加如下代码
<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="TextImageView"><attr name="drawableStartWidth" format="dimension"/><attr name="drawableStartHeight" format="dimension"/><attr name="drawableTopWidth" format="dimension"/><attr name="drawableTopHeight" format="dimension"/><attr name="drawableEndWidth" format="dimension"/><attr name="drawableEndHeight" format="dimension"/><attr name="drawableBottomWidth" format="dimension"/><attr name="drawableBottomHeight" format="dimension"/></declare-styleable>
</resources>
自定义TextView控件
/*** create by 薛瑄* time 2019/5/21 15:20*/
class TextImageView : AppCompatTextView {private var mStartWidth: Int = 0private var mStartHeight: Int = 0private var mTopWidth: Int = 0private var mTopHeight: Int = 0private var mEndWidth: Int = 0private var mEndHeight: Int = 0private var mBottomWidth: Int = 0private var mBottomHeight: Int = 0constructor(context: Context) : super(context) {}constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {init(context, attrs)}constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {init(context, attrs)}fun init(context: Context, attrs: AttributeSet) {val typedArray = context.obtainStyledAttributes(attrs, R.styleable.TextImageView)mStartWidth = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableStartWidth, 0)mStartHeight = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableStartHeight, 0)mTopWidth = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableTopWidth, 0)mTopHeight = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableTopHeight, 0)mEndWidth = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableEndWidth, 0)mEndHeight = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableEndHeight, 0)mBottomWidth = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableBottomWidth, 0)mBottomHeight = typedArray.getDimensionPixelOffset(R.styleable.TextImageView_drawableBottomHeight, 0)typedArray.recycle()setDrawablesSize()}private fun setDrawablesSize() {val compoundDrawables = compoundDrawablesRelativefor (i in compoundDrawables.indices) {when (i) {0 -> setDrawableBounds(compoundDrawables[0], mStartWidth, mStartHeight)1 -> setDrawableBounds(compoundDrawables[1], mTopWidth, mTopHeight)2 -> setDrawableBounds(compoundDrawables[2], mEndWidth, mEndHeight)3 -> setDrawableBounds(compoundDrawables[3], mBottomWidth, mBottomHeight)else -> {}}}setCompoundDrawablesRelative(compoundDrawables[0],compoundDrawables[1],compoundDrawables[2],compoundDrawables[3])}private fun setDrawableBounds(drawable: Drawable?, width: Int, height: Int) {if (drawable != null) {drawable.setBounds(0, 0, width, height)if (width == 0 || height == 0) {val scale = drawable.intrinsicHeight.toDouble() / drawable.intrinsicWidth.toDouble()val bounds = drawable.bounds//高宽只给一个值时,自适应if (width == 0 && height != 0) {bounds.right = (bounds.bottom / scale) as Intdrawable.bounds = bounds}if (width != 0 && height == 0) {bounds.bottom = (bounds.right * scale) as Intdrawable.bounds = bounds}}}}
}
参考:
https://www.jianshu.com/p/09a5399dc26b
TextView 在xml 中设置图片大小相关推荐
- ViewPager 中设置图片大小不起作用
需求是一个手动图片滑动效果,但是图片不填充整个屏幕,同时滑动图片的时候,图片是从左右的最边上滑过来的.于是想到viewpger:将viewpager填充整个屏幕,但是将图片设置成想要的大小,这样效果就 ...
- plot.figue中设置图片的大小
import matplotlib.pyplot as plt import matplotlib.pylab as pylab import numpy as npdef draw_bar(self ...
- java 在线给word 文件插入图片,并设置图片大小
一.前言: 有时我们想在一篇word文档中的指定位置添加一张服务器上的图片,并且并且设置图片大小.这个对文档的操作一定是需要第三方api的.市面上对文档处理的插件有POI以及openoffice等等, ...
- 【Flutter】Flutter 启动白屏问题 ( 问题描述 | 在 launch_background.xml 中设置启动过渡 UI )
文章目录 一.Flutter 启动白屏问题 二.在 launch_background.xml 中设置启动过渡 UI 三.博客源码 一.Flutter 启动白屏问题 启动 Flutter 应用 , 在 ...
- 如何批量处理Word中的图片大小
用宏批量定位word中图片位置 word文档里面有多幅图,但图片的缩进位置不统一,有些靠右,有些居中,有些靠左等等. 能否通过编制宏一次完成调整,让所有图片靠左,或居中. 通过宏可以批量完成一 ...
- word 批量设置图片大小
word批量修改图片大小--固定长宽篇 方法一: 这部分要说的是把word中的所有图片修改成固定的并且相同的长和宽! 1.打开word,工具-宏-宏(或者直接按Alt+F8)进入宏的 界面,如下面所示 ...
- caffe ssd中输入图片大小对于内存使用和运行时间的影响
caffe ssd中输入图片大小对于内存使用和运行时间的影响 一.内存使用 环境为caffe下以imagenet为样本集训练resnset网络. 在初始化生成lmdb的create_ima ...
- 在Word中调整图片大小会自动改变怎么办?
在Word中调整图片大小会自动改变怎么办? 在Word中有多个图片,需要调整到大小一致,可是拉动.或者设置图片大小后,图片会自动改变,始终无法将所有图片调到大小一致. 别担心 鼠标右键点击图片 选择大 ...
- html img图片不平铺,如何在css中设置图片不平铺
如何在css中设置图片不平铺 发布时间:2021-04-29 15:39:28 来源:亿速云 阅读:80 作者:Leah 这篇文章将为大家详细讲解有关如何在css中设置图片不平铺,文章内容质量较高,因 ...
- matplotlib折线图(设置图片大小和图片保存)
代码示例: from matplotlib import pyplot as plt x = range(1,10) #x轴的位置 y = [6,7,12,12,15,17,15,20,18] #y轴 ...
最新文章
- Educational Codeforces Round 80 (Rated for Div. 2)SZU cf集训round2 C~E(dp,状压+二分,树状数组+逆向思维)
- Java 8 中的流操作-基本使用性能测试
- 一目了然“之IT运维可视化,让运维不再说难!
- TCP/IP协议之网络链接的背后故事
- 网站502与504错误分析
- Python6:oriented objective programming
- Dropwizard入门及开发步骤
- 连接moogDB数据库
- 使用flex和bison实现的sql引擎解析
- 《三国志》生僻人名读法集(zz)
- Android文件夹占用空间分析,关于Android中图片大小、内存占用与drawable文件夹关系的研究与分析...
- 高精度计时器 -- C++/Windows版
- [SQL]LeetCode180. 连续出现的数字 | Consecutive Numbers
- 【20保研】山东师范大学信息科学与工程学院2019年 “优秀大学生暑期夏令营”活动公告...
- 自我监督学习:提高深度学习数据效率的计划
- MySQL讲义第 46 讲——select 查询之查询练习(四)
- 求职简历中一些常见的问题
- hexo更换icarus主题和美化
- QtScrcpy手机投屏电脑利器连接Android设备
- springboot读取bootstrap配置及knife4j版本兼容性问题