210

针对我的代码运行新的 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>

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

4

4 回答 4

279

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

在您的情况下,您根本不需要LinearLayoutand ImageView。只需添加android:drawableLeft="@drawable/up_count_big"到您的TextView.

See TextView#setCompoundDrawablesWithIntrinsicBounds for more info.

于 2011-11-29T23:20:01.517 回答
21

if for some reason you need to add via code, you can use this:

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

where left, top, right bottom are Drawables

于 2013-12-11T14:32:00.900 回答
3

To add to this - it seems important to define the width & height of the drawable as per this post:

(his code works)

于 2012-11-22T10:05:05.767 回答
1

You can use general compound drawable implementation, but if you need to define a size of drawable use this library:

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

Here is a small example of usage:

<com.tolstykh.textviewrichdrawable.TextViewRichDrawable
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Some text"
        app:compoundDrawableHeight="24dp"
        app:compoundDrawableWidth="24dp" />
于 2016-12-27T14:33:24.217 回答