Android 在TextView中以编程方式使用Drawable对象的DrawableLeft

Android 在TextView中以编程方式使用Drawable对象的DrawableLeft,android,bitmap,textview,android-drawable,Android,Bitmap,Textview,Android Drawable,目前正在开发客户端和服务器之间的文本和图像的新视图 我为行视图创建了row.xml,如下所示: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/msgLayout" andro

目前正在开发客户端和服务器之间的文本和图像的新视图

我为行视图创建了row.xml,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/msgLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/msgText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5sp"
        android:background="@drawable/speech_bubble_green"
        android:drawableTop="@drawable/ic_content_picture"
        android:shadowColor="@color/textShadow"
        android:shadowDx="1"
        android:shadowDy="1"
        android:textSize="0sp" />

</LinearLayout>
现在,我想在获取要发送或接收的图像时,以编程方式设置drawableTop,而不使用文本

发送时,我从图库中拾取图像,它返回位图对象

如何以编程方式将位图对象设置为drawableTop

附笔。 我已经看到了这个答案,但它适用于可绘制Id

非常感谢您的帮助。

您可以使用以下方法:

Drawable d = new BitmapDrawable(bm);
d.setBounds(0, 0, bm.getWidth(), bm.getHeight());
textView.setCompoundDrawables(null, d, null, null);

+1因此,您键入了此答案,而不是抄写;BitmapDrawableBitmap位图构造函数已弃用。