Android - set layout background programmatically

后端 未结 3 1371
旧巷少年郎
旧巷少年郎 2020-12-09 21:04

I have noticed that the setBackground method for the RelativeLayout object is targeted for API 16 (Android 4.1) and higher, but my application has the targ

3条回答
  •  失恋的感觉
    2020-12-09 21:37

    It depends. If you want to set a color as the background, use setBackgroundColor(). For a Drawable, you can use the now deprecated method setBackgroundDrawable() for APIs below 16, and setBackground() for API 16 devices. You can also use setBackgroundResource() for setting a resource as the background.

    Note that while a lot of methods are marked as deprecated, I'm yet to come across one that has actually been removed. So while you could use the deprecated method even in API 16, I'd recommend setting your target API to 16 and using an if else to switch between the methods, depending on the API version the device is running.

提交回复
热议问题