
AndroID 中TextVIEw的使用imagevIEw被压缩问题解决办法
看下运行效果图:
今天解BUG的时候遇到一个奇怪的问题:ListvIEw的item由一个textvIEw和一个imagevIEw组成,父布局是线性水平排列。我的本意是imagevIEw显示相同的图片,textvIEw显示文本,但是运行程序后发现,当某个textvIEw的文本较多时,imagevIEw会被压缩,刚开始没注意,检查代码了好久。
代码示例如下:
<!--文本少的item--><linearLayout androID:ID="@+ID/ll" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:background="#e6e9ed" androID:gravity="center_vertical|right"> <TextVIEw androID:ID="@+ID/Title" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:gravity="bottom" androID:text="我们右边引用的是同一张图片" androID:textSize="16sp" /><ImageVIEw androID:ID="@+ID/icon" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginleft="17dp" androID:layout_marginRight="23dp" androID:background="@drawable/test" /></linearLayout><!--文本多的item--><linearLayout androID:ID="@+ID/ll" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_margintop="50dp" androID:background="#e6e9ed" androID:gravity="center_vertical|right"> <TextVIEw androID:ID="@+ID/Title" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:gravity="bottom" androID:text="我们右边引用的是同一张图片,我字数多" androID:textSize="16sp" /><ImageVIEw androID:ID="@+ID/icon" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginleft="17dp" androID:layout_marginRight="23dp" androID:background="@drawable/test" /></linearLayout>
可以发现,第二个布局中,右边图片被“挤扁”了。为什么会出现这种情况?其实很简单,是textvIEw宽度自适应搞的鬼。水平线形布局中,我们虽然设置了imagevIEw与左右的偏移(margin)值,但是由于自布局textvIEw与imagevIEw是按顺序排列的,textvIEw会首先完成它的自适应,导致字数过多的时候会把右边的imagevIEw压缩,此时imagevIEw的左右margin值还是我们设置的。
那么,怎么设置才能让文本框显示较多文字而又不挤压右边的imagevIEw呢?
答案很简单,还是要在textvIEw的宽度上做文章了。只需要:
textvIEw的wIDth属性依然设置为:wrap_content自适应,再加上一个权重属性即可:weight="1".这样,textvIEw就会占据水平剩下的空间,而不会去挤压右边的imageivew了。
代码如下:
<linearLayout androID:ID="@+ID/ll" androID:layout_wIDth="match_parent" androID:layout_height="wrap_content" androID:layout_margintop="50dp" androID:background="#e6e9ed" androID:gravity="center_vertical|right"> <TextVIEw androID:ID="@+ID/Title" androID:layout_wIDth="wrap_content" androID:layout_height="match_parent" androID:layout_weight="1" androID:ellipsize="end" androID:gravity="bottom" androID:singleline="true" androID:text="我们右边引用的是同一张图片,我字数多" androID:textSize="16sp" /> <ImageVIEw androID:ID="@+ID/icon" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_marginleft="17dp" androID:layout_marginRight="23dp" androID:background="@drawable/test" />
运行效果就正常了:
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
总结以上是内存溢出为你收集整理的Android 中TextView的使用imageview被压缩问题解决办法全部内容,希望文章能够帮你解决Android 中TextView的使用imageview被压缩问题解决办法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)