在Android 应用中使用TextView时出现imageview被压缩如何解决

发布时间:2020-12-02 17:03:18 作者:Leah
来源:亿速云 阅读:202

在Android 应用中使用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被压缩如何解决的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. Android性能优化:ViewStub
  2. 如何成为一个偷懒又高效的Android开发人员

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

android textview imageview

上一篇:如何使用InputStream字节流

下一篇:如何在Android应用中实现一个红点拖拽删除功能

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》