2011-07-06 34 views
248

Sau khi tôi gọi phương thức setCompoundDrawables, hợp chất drawable không được hiển thị ..setCompoundDrawables Gọi() không hiển thị Compound drawable

Drawable myDrawable = getResources().getDrawable(R.drawable.btn); 
btn.setCompoundDrawables(myDrawable, null, null, null); 

Bất kỳ suy nghĩ?

+7

Như đã nêu trong câu trả lời bên dưới, biến thể của phương thức có tên '(..) WithIntrinsicBounds' cần được gọi. Trên một lưu ý phụ, 'padding' cho Drawable Compound phải được đặt ** sau ** cuộc gọi này để gây ra hiệu ứng – Dr1Ku

+3

[document] (http://developer.android.com/reference/android/widget/TextView. html # setCompoundDrawables% 28android.graphics.drawable.Drawable,% 20android.graphics.drawable.Drawable,% 20android.graphics.drawable.Drawable,% 20android.graphics.drawable.Drawable% 29) nói: * Bản vẽ phải có ['setBounds (Rect)'] (http://developer.android.com/reference/android/graphics/drawable/Drawable.html#setBounds (android.graphics.Rect)) được gọi. * –

+0

hi hunterp, vừa gặp bạn tại quán cà phê (Angel), bây giờ tôi biết bạn biết Android Drawables là gì (và có lẽ bạn đã đánh cắp bộ nhớ lỗi khi làm việc với nhiều người trong số họ), tôi có thể nói với bạn về một số dự án tôi đã cộng tác đã phải đối phó với vấn đề này, hãy kiểm tra https://github.com/JakeWharton/DiskLruCache (mà tôi đã cộng tác để làm cho android thân thiện hơn) được sử dụng bởi Picasso (https://github.com/square/picasso) – Gubatron

Trả lời

41

Hình ảnh trống vì không có giới hạn được chỉ định. Bạn có thể sử dụng setCompoundDrawables() nhưng trước khi bạn chỉ định giới hạn của hình ảnh, sử dụng phương pháp Drawable.setBounds()

+0

Câu trả lời hay nhất bởi vì bạn thực sự cung cấp lý do là tại sao setBounds lại quan trọng. – Andy

54

Sử dụng tính năng này (Tôi đã kiểm tra). Nó hoạt động tốt

Drawable image = context.getResources().getDrawable(R.drawable.ic_action); 
int h = image.getIntrinsicHeight(); 
int w = image.getIntrinsicWidth(); 
image.setBounds(0, 0, w, h); 
button.setCompoundDrawables(image, null, null, null); 
+0

giúp, cảm ơn bạn –

+1

Điều này hữu ích khi nhắm mục tiêu API thấp hơn 17, vì 'EditText # setCompoundDrawablesWithIntrinsicBounds' yêu cầu ít nhất API 17. –

+4

Bạn có thể cung cấp nguồn cho điều đó không? Tất cả tài liệu tôi đã thấy cho biết đây là [có sẵn từ API 1] (https://developer.android.com/reference/android/widget/TextView.html). – kurifu

20

Một chút đơn giản hơn nữa:

Drawable image = context.getResources().getDrawable(R.drawable.ic_action); 
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight()); 
button.setCompoundDrawables(image, null, null, null); 
7

nó bị phản đối trong API 22.

Mã này rất hữu ích cho tôi:

Drawable drawable = ResourcesCompat.getDrawable(getResources(),R.drawable.wen, null); 
drawable.setBounds(0, 0, drawable.getMinimumWidth(), 
drawable.getMinimumHeight()); 
tv.setCompoundDrawables(drawable, null, null, null); 
15

Ví dụ bộ lên trên cùng:

view.setCompoundDrawablesWithIntrinsicBounds(
        null, getResources().getDrawable(R.drawable.some_img), null, null); 

thứ tự đối số: (trái, trên, phải, dưới)

+2

Điều này nên được đưa lên đầu –

Các vấn đề liên quan