在我的代码上运行了新的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>

有人能提供一个具体的例子,如何使一个化合物可绘制在这种情况下?


当前回答

TextView附带了4个复合绘图,分别为左,上,右和底。

在你的例子中,你根本不需要LinearLayout和ImageView。只需添加android: drawablleft ="@drawable/up_count_big"到你的TextView。

更多信息见TextView#setCompoundDrawablesWithIntrinsicBounds。

其他回答

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

与android:drawableBottom属性结合使用时,无法显示可绘制的形状。

(他的代码有效)

TextView附带了4个复合绘图,分别为左,上,右和底。

在你的例子中,你根本不需要LinearLayout和ImageView。只需添加android: drawablleft ="@drawable/up_count_big"到你的TextView。

更多信息见TextView#setCompoundDrawablesWithIntrinsicBounds。

你可以使用一般的复合drawable实现,但如果你需要定义drawable的大小,请使用以下库:

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" />

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

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

左,上,右,底部是Drawables