无意间遇到的TextView的一个坑
来源:互联网 发布:淘宝买家秀福利怎么找 编辑:程序博客网 时间:2024/06/05 03:23
今天解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" />
运行效果就正常了:
0 0
- 无意间遇到的TextView的一个坑
- 无意间的!
- 无意间发现的一个帖子 ---转贴---
- 无意间发现的一个帖子 ---转贴---
- 无意间的发现
- 无意间学到的一个关于html的小知识点
- 竟然无意间找到一个国人做的v3d教学录像
- 无意间发现的一个帖子,很有感触
- 无意间创建的一个闭包,以及解决办法
- 无意间发现的有趣事
- 无意间碰到的乱码问题
- recyclerview中的 TextView 跑马灯遇到的一个问题
- android TextView遇到的问题
- 无意间发现的的搜索的特效
- 遇到的一个坑
- 昨天做界面时无意间发现IE7显示的一个bug
- 无意间发现一个写得较好的JavaScript日期挑选控件
- 无意间看到一个留学生大牛的Open IMS Core战略思想-------太牛叉了!!!!------致敬
- 事件分发主要过程
- 微信小程序学习一初探“小程序”
- 使用腾讯bugly实现应用自动更新提示
- 最近太忙了
- shallow copy 和 deep copy 的示例
- 无意间遇到的TextView的一个坑
- APP三种开发模式--之--HybridApp解决方案
- Android自带类实现语音合成
- hellocharts-android-Android图表开源库的使用(一)
- 关于cpu的core、die、pacaage的解释
- hellocharts-android-Android图表开源库的使用(二)
- Android USB转串口通信开发基本流程
- Java 集合 5:HashSet ,TreeSet 实现原理
- iOS打开闪光灯