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

 更新时间:2017年04月10日 14:27:31   投稿:lqh  
这篇文章主要介绍了Android 中TextView的使用imageview被压缩问题解决办法的相关资料,需要的朋友可以参考下

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自定义View构造函数详解

    Android自定义View构造函数详解

    这篇文章主要为大家详细介绍了Android自定义View构造函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-10-10
  • android时间选择控件之TimePickerView使用方法详解

    android时间选择控件之TimePickerView使用方法详解

    这篇文章主要为大家详细介绍了android时间选择控件之TimePickerView的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-09-09
  • Android入门之IntentService的使用教程详解

    Android入门之IntentService的使用教程详解

    IntentService的生命周期中有一个非常好的方法-onHandleIntent方法,它是一个abstract方法,开发者在实现IntentService时可以覆盖它来处理“长事务”。本文就来聊聊IntentService的使用,需要的可以参考一下
    2022-12-12
  • Android实现界面跳转功能

    Android实现界面跳转功能

    这篇文章主要为大家详细介绍了Android实现界面跳转功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2020-09-09
  • Android将图片上传到php服务器的实例代码

    Android将图片上传到php服务器的实例代码

    这篇文章主要介绍了Android将图片上传到php服务器的实例代码,需要的朋友可以参考下
    2017-07-07
  • iOS UIButton 点击无响应的解决办法

    iOS UIButton 点击无响应的解决办法

    在开发中按钮我们经常会遇到,但是有时候会碰到一些难以处理的问题,就是按钮点击无响应,其实解决方法也不难。下面小编之家小编抽空给大家介绍iOS UIButton 点击无响应的解决办法,需要的朋友参考下吧
    2017-12-12
  • Android中google Zxing实现二维码与条形码扫描

    Android中google Zxing实现二维码与条形码扫描

    这篇文章主要介绍了Android中google Zxing实现二维码与条形码扫描的相关资料,需要的朋友可以参考下
    2017-05-05
  • Android中TabLayout添加小红点的示例代码

    Android中TabLayout添加小红点的示例代码

    本篇文章主要介绍了Android中TabLayout添加小红点的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • Android Studio SVN使用方法教程

    Android Studio SVN使用方法教程

    这篇文章主要介绍了Android Studio SVN使用方法教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-09-09
  • Android程序开发如何处理图像格式类及图像转换

    Android程序开发如何处理图像格式类及图像转换

    这篇文章主要介绍了Android程序开发如何处理图像格式类及图像转换,需要的朋友可以参考下
    2015-07-07

最新评论