Android 中TextView的使用imageview被压缩问题解决办法

所属分类: 软件编程 / Android 阅读数: 52
收藏 0 赞 0 分享

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样式和主题之选择器的实例讲解

今天小编就为大家分享一篇关于Android样式和主题之选择器的实例讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Android应用动态修改主题的方法示例

今天小编就为大家分享一篇关于Android应用动态修改主题的方法示例,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter 网络请求框架封装详解

这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Flutter倒计时/计时器的实现代码

这篇文章主要介绍了Flutter倒计时/计时器的实现代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Android使用google breakpad捕获分析native cash

这篇文章主要介绍了Android使用google breakpad捕获分析native cash 的相关知识,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

详解Flutter WebView与JS互相调用简易指南

这篇文章主要介绍了详解Flutter WebView与JS互相调用简易指南,分为JS调用Flutter和Flutter调用JS,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android开发实现ListView和adapter配合显示图片和文字列表功能示例

这篇文章主要介绍了Android开发实现ListView和adapter配合显示图片和文字列表功能,涉及Android使用ListView结合adapter适配器实现图文显示功能相关的布局、解析、权限控制等操作技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android文字基线Baseline算法的使用讲解

今天小编就为大家分享一篇关于Android文字基线Baseline算法的使用讲解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
收藏 0 赞 0 分享

Flutter自定义实现神奇动效的卡片切换视图的示例代码

这篇文章主要介绍了Flutter自定义实现神奇动效的卡片切换视图的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Android实现自定义滑动刻度尺方法示例

这篇文章主要给大家介绍了关于Android实现自定义滑动刻度尺的相关资料,文中通过示例代码介绍的非常详细,对各位Android开发者们具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
收藏 0 赞 0 分享
查看更多