【问题标题】:How do I use a compound drawable instead of a LinearLayout that contains an ImageView and a TextView如何使用复合可绘制对象而不是包含 ImageView 和 TextView 的 LinearLayout
【发布时间】:2012-01-09 06:21:49
【问题描述】:

针对我的代码运行新的 Lint 工具。它提出了很多好的建议,但我无法理解。

这个标签和它的子标签可以被一个和一个复合drawable替换

问题:检查当前节点是否可以使用复合可绘制对象替换为 TextView。

包含 ImageView 和 TextView 的 LinearLayout 可以更有效地处理为复合可绘制对象

这是我的布局

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerInParent="true">

<ImageView 
    android:id="@+id/upImage"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:layout_gravity="center_vertical"
    android:scaleType="centerInside"
    android:src="@drawable/up_count_big">
</ImageView>

<TextView
    android:id="@+id/LikeCount"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="2dp"
    android:layout_marginBottom="1dp"
    android:textColor="@color/gray"
    android:textSize="16sp"
    android:layout_gravity="center_vertical">
</TextView>
</LinearLayout>

有人可以提供一个具体的例子来说明在这种情况下如何使复合可绘制吗?

【问题讨论】:

  • fwiw 这是我通常禁用的检查之一,因为误报。并非所有TextView / ImageView 组合都可以用这种方式替换。
  • @RichardLeMesurier 如果要定义图像大小,可以使用自定义视图。这将使您的视野更轻。看我的回答:stackoverflow.com/a/31916731/2308720
  • @Alex 我在很多情况下都同意,但是根据我的经验,我经常不值得费心创建自定义视图来绕过我认为有问题的 Lint 检查。

标签: android android-layout android-linearlayout textview compound-drawables


【解决方案1】:

TextView 带有 4 个复合可绘制对象,左、上、右和下各一个。

就您而言,您根本不需要LinearLayoutImageView。只需将android:drawableLeft="@drawable/up_count_big" 添加到您的TextView

请参阅TextView#setCompoundDrawablesWithIntrinsicBounds 了解更多信息。

【讨论】:

  • 不一定,因为您的链接显示了带有动态图像的示例。在这种情况下,使用ImageView 可能仍然更容易或更可取。警告说“可以被替换”,而不是“应该被替换”
  • 但是如何在图像和文本之间留出一些空间
  • @Hitesh Dhamshaniya,在 XML 或代码中使用 DrawablePadding 属性。
  • 在我的情况下,我移动到带有内部 ImageView/TextView 的 LinearLayout 因为 android:drawableLeft 没有提供足够的控制——所以,你基本上告诉我的是这个警告/建议是谎言。
  • 这不是谎言,在某些情况下,单个 TextView 确实是有效的。但总的来说 - 是的,带有 TextView 的 ImageView 提供了更丰富的控制。
【解决方案2】:

如果由于某种原因需要通过代码添加,可以使用这个:

mTextView.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom);

其中左、上、右下是可绘制对象

【讨论】:

  • 需要 API 17 以上
  • 我不这么认为,documentation 自 API 1 以来的状态
  • 查看此链接 [文档](developer.android.com/reference/android/widget/…, android.graphics.drawable.Drawable, android.graphics.drawable.Drawable, android.graphics.drawable.Drawable))
  • 是的,请注意方法名称中的 Relative ;) 我在答案中没有提到这个
【解决方案3】:

补充一点 - 根据这篇文章定义可绘制对象的宽度和高度似乎很重要:

(他的代码有效)

【讨论】:

    【解决方案4】:

    您可以使用通用的复合可绘制实现,但如果您需要定义可绘制的大小,请使用此库:

    https://github.com/a-tolstykh/textview-rich-drawable

    下面是一个小例子:

    <com.tolstykh.textviewrichdrawable.TextViewRichDrawable
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Some text"
            app:compoundDrawableHeight="24dp"
            app:compoundDrawableWidth="24dp" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-15
      • 1970-01-01
      • 2021-02-15
      • 2012-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多