TextView setCompoundDrawables不显示

本文记录了一个关于Android应用中图标无法正常显示的问题及解决方案。通过设置Drawable的边界大小,成功使图标在TextView中正确显示。

记录一个bug:

执行代码:
Drawable openIcon = context.getResources().getDrawable(R.drawable.drip_mail_opened);
holder.timeTv.setCompoundDrawables(openIcon, null, null, null);
Icon不显示;

修改如下:
Drawable openIcon = context.getResources().getDrawable(R.drawable.drip_mail_opened);
openIcon.setBounds(0, 0, 24, 24);
holder.timeTv.setCompoundDrawables(openIcon, null, null, null);
则Icon显示正常。

### TextView 显示文字全的解决方案 在 Android 开发中,`TextView` 是最常用的控件之一,用于显示文本内容。然而,在某些布局或文本内容复杂的情况下,会出现 `TextView` 文字显示全的问题,这通常与布局嵌套、换行策略、最大行数限制以及文本内容本身的特性有关。 #### 1. 布局嵌套导致的显示全 当 `TextView` 与 `CheckBox`、`ImageView` 等控件一同嵌套在 `LinearLayout` 中时,如果 `TextView` 的内容超过一行,可能出现最后一行显示全的问题。这种现象通常是因为 `TextView` 与父容器之间存在未设置的顶部间距,而这种间距并非来自显式设置的 `margin` 或 `padding`,而是由系统默认的排版逻辑导致的。解决方法包括: - 使用 `android:layout_weight` 来分配 `TextView` 的空间,确保其能够完整显示多行文本。 - 将 `LinearLayout` 替换为 `ConstraintLayout`,以获得更灵活的布局控制。 - 设置 `TextView` 的 `android:gravity` 属性为 `center_vertical` 或 `top`,以调整其在容器中的对齐方式[^1]。 ```xml <TextView android:id="@+id/textView" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center_vertical" android:text="多行文本内容" /> ``` #### 2. 最大行数与省略策略限制导致的显示全 当 `TextView` 设置了 `maxLines` 和 `ellipsize` 属性时,如果文本内容较多,可能会出现文字被截断或省略号显示正确的问题。即使设置了 `ellipsize="end"`,在某些设备或系统版本上仍可能无效。建议的解决方案包括: - 明确设置 `TextView` 的高度为 `wrap_content`,并确保父容器会限制其高度。 - 避免使用 `ellipsize`,改用动态计算文本行数并手动截断的方式。 - 使用 `android:inputType="textMultiLine"` 来强制 `TextView` 支持多行显示[^2]。 ```xml <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxLines="3" android:inputType="textMultiLine" android:text="这是一段很长的文本内容,可能需要多行显示" /> ``` #### 3. 自动换行策略导致的显示异常 在多行 `TextView` 中,如果设置了 `android:breakStrategy` 属性,但仍然出现文字显示全的情况,可能是由于系统换行策略未能正确识别中英文混合或标点符号边界。可以通过设置 `android:breakStrategy="simple"` 来避免复杂换行策略导致的显示问题[^3]。 ```xml <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:breakStrategy="simple" android:text="中英文混合文本内容,例如Hello World中文内容" /> ``` #### 4. 图文混排时的显示问题 当使用 `setCompoundDrawables` 向 `TextView` 添加图片时,若未正确设置 `Drawable` 的边界,可能导致图片显示,从而影响整体文本的视觉呈现。解决方法是在设置图片前调用 `setBounds` 方法以确保 `Drawable` 正确绘制: ```java Drawable drawable = ContextCompat.getDrawable(context, R.drawable.icon); drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight()); textView.setCompoundDrawables(drawable, null, null, null); ``` #### 5. 字体与排版设置影响显示 在某些字体渲染策略下,系统可能会因字体内边距(`android:includeFontPadding`)的存在导致文字上下留白过多,影响多行文本的完整显示。可以通过关闭该属性来优化显示效果: ```xml <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:includeFontPadding="false" android:text="包含空格的文本" /> ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值