2014-11-26 23 views
45

Vì vậy, sdk của tôi đi từ 15 đến 21 và khi tôi gọi setBackgroundDrawable(), Android Studio cho tôi biết rằng nó không được chấp nhận.setBackgroundDrawable() không được chấp nhận

tôi nghĩ đi xung quanh nó bằng cách sử:

int sdk = android.os.Build.VERSION.SDK_INT; 

if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) { 
    layout.setBackgroundDrawable(getResources().getDrawable(R.drawable.img_wstat_tstorm)); 
} else { 
    layout.setBackground(getResources().getDrawable(R.drawable.img_wstat_tstorm)); 
} 

Nhưng sau đó, tôi nhận được một lỗi tại "setBackground()".

Vì vậy, bạn sẽ xử lý nó như thế nào?

+0

Bạn gặp lỗi hoặc cảnh báo? –

+0

giá trị nào bạn có trong phiên bản sdk tối thiểu trong tệp kê khai? – Manmohan

+3

sử dụng setbackgroundresource (R.drawable.img_wstat_tstorm); cho phiên bản cao hơn.setBackgroundDrawable được depricated trong verion cao hơn, hy vọng này giúp bạn – prakash

Trả lời

56

Đây là một chủ đề thú vị. Cách bạn đang làm nó là chính xác, rõ ràng. Nó thực sự chỉ là một sự thay đổi quyết định đặt tên. Như this answer chỉ ra, setBackground() chỉ gọi setBackgroundDrawable():

public void setBackground(Drawable background) { 
    //noinspection deprecation 
    setBackgroundDrawable(background); 
} 

@Deprecated 
public void setBackgroundDrawable(Drawable background) { ... } 

Bạn có thể thấy this thread để biết thêm thông tin về tất cả điều này.

+12

Bạn nên lưu ý rằng 'setBackground()' sẽ không hoạt động đối với API16 trước, một giải pháp thay thế có thể là 'setBackgroundResource' – Mood

2

Bạn đang gặp lỗi vì getResources(). GetDrawable() lấy một id (int) không thể vẽ được làm đối số của nó. Hãy thử điều này:

layout.setBackground(getResources().getDrawable(R.id.img_wstat_tstorm));

+0

setBackground mong * Chỉ * Id chỉ –

+0

Bạn không chính xác. Từ tài liệu API: android.view.View.setBackground (Nền có thể vẽ); Tham số: nền Có thể vẽ để sử dụng làm nền hoặc null để xóa nền. –

13

Thật buồn cười vì phương pháp đó được chấp nhận, nhưng nếu bạn nhìn vào Source Code Android, bạn sẽ tìm thấy điều này:

/** 
    * Set the background to a given Drawable, or remove the background. If the 
    * background has padding, this View's padding is set to the background's 
    * padding. However, when a background is removed, this View's padding isn't 
    * touched. If setting the padding is desired, please use 
    * {@link #setPadding(int, int, int, int)}. 
    * 
    * @param background The Drawable to use as the background, or null to remove the 
    *  background 
    */ 
    public void setBackground(Drawable background) { 
     //noinspection deprecation 
     setBackgroundDrawable(background); 
    } 
14

có lẽ bạn có thể thử như sau:

setBackgroundResource(R.drawable.img_wstat_tstorm); 
0

tôi đang sử dụng một minSdkVersion 16 và targetSdkVersion 23 sau đây là làm việc cho tôi, nó sử dụng ContextCompat.getDrawable (context, R.drawable.drawable);

Thay vì sử dụng: layout.setBackground(getResources().getDrawable(R.drawable.img_wstat_tstorm));

Thay sử dụng:

layout.setBackground(ContextCompat.getDrawable(getActivity(), R.drawable.img_wstat_tstorm));

getActivity() được sử dụng trong một mảnh, nếu gọi từ việc sử dụng hoạt động this

+0

Câu hỏi được yêu cầu cho minSdk 15 –

1

này là chính xác trong trường hợp của tôi Giải quyết vấn đề này

imageView.setBackgroundResource(images[productItem.getPosition()]); 
-1
BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.mipmap.Nome_imgem)); 
     getSupportActionBar().setBackgroundDrawable(background); 
+0

Nó thực sự sẽ giúp đỡ nếu bạn sẽ quấn lên trong một vài từ những gì bạn đang cố gắng làm ở đây ... – arkascha

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