2013-06-03 135 views
0

我有以下的TextView佈局:加載圖像到TextView的背景

<TextView 
    android:layout_width="10dp" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:drawableTop="@drawable/ic_launcher" > 

我誇大這個佈局中的代碼,並希望使用Android系統的通用 - 圖像 - 加載到圖像加載到drawableTop。但是,據我所知,沒有辦法從TextView獲取backgroundTop的ImageView,因此我不能使用AUIL。有沒有辦法讓ImageView獲得背景,如果沒有,我應該如何修改佈局,使其具有ImageView,但看起來與原始佈局相同?

回答

0

使用RelativeLayoutImageViewTextView然後操縱ImageView

<RelativeLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="0dp" 
     android:layout_height="0dp" 
     android:layout_alignBottom="@+id/textView1" 
     android:layout_alignLeft="@+id/textView1" 
     android:layout_alignRight="@+id/textView1" 
     android:layout_alignTop="@+id/textView1" 
     android:src="@drawable/ic_launcher" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" /> 
</RelativeLayout> 
0
   TextView tv = (TextView) findViewById(R.id.TV); 
       Drawable top = getResources().getDrawable(R.drawable.icon) ; 
       tv.setCompoundDrawablesWithIntrinsicBounds(null, top, null, null) ; 
//    tv.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom) 
8

您可以使用監聽器(ImageLoadingListener)了點。將以下代碼放入onLoadingComplete(...)

void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) { 
    Drawable topImage = new BitmapDrawable(loadedImage); 
    textView.setCompoundDrawablesWithIntrinsicBounds(null, topImage, null null); 
} 
+2

這應該是公認的答案。 –