如何在android中设置背景,如何在Android中以编程方式设置背景可绘制
MMTTMM
layout.setBackgroundResource(R.drawable.ready);是正確的。實(shí)現(xiàn)它的另一種方法是使用以下方法:final int sdk = android.os.Build.VERSION.SDK_INT;if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {? ? layout.setBackgroundDrawable(ContextCompat.getDrawable(context, R.drawable.ready) );} else {? ? layout.setBackground(ContextCompat.getDrawable(context, R.drawable.ready));}但我認(rèn)為問題出現(xiàn)是因?yàn)槟趪L試加載大圖像。這是一個(gè)很好的教程如何加載大位圖。更新:API級(jí)別22中不推薦使用的getDrawable(int)?getDrawable(int )現(xiàn)在已在API級(jí)別22中棄用。您應(yīng)該使用支持庫中的以下代碼:ContextCompat.getDrawable(context, R.drawable.ready)如果你引用ContextCompat.getDrawable的源代碼,它會(huì)給你這樣的東西:/**?* Return a drawable object associated with a particular resource ID.?*
?* Starting in {@link android.os.Build.VERSION_CODES#LOLLIPOP}, the returned?* drawable will be styled for the specified Context's theme.?*?* @param id The desired resource identifier, as generated by the aapt tool.?*? ? ? ? ? ? This integer encodes the package, type, and resource entry.?*? ? ? ? ? ? The value 0 is an invalid identifier.?* @return Drawable An object that can be used to draw this resource.?*/public static final Drawable getDrawable(Context context, int id) {? ? final int version = Build.VERSION.SDK_INT;? ? if (version >= 21) {? ? ? ? return ContextCompatApi21.getDrawable(context, id);? ? } else {? ? ? ? return context.getResources().getDrawable(id);? ? }}關(guān)于ContextCompat的更多細(xì)節(jié)?從API 22開始,您應(yīng)該使用該getDrawable(int, Theme)方法而不是getDrawable(int)。更新:如果您使用的是支持v4庫,則以下內(nèi)容對(duì)所有版本都足夠了。ContextCompat.getDrawable(context, R.drawable.ready)您需要在app build.gradle中添加以下內(nèi)容compile 'com.android.support:support-v4:23.0.0' # or any version above或者在以下任何API中使用ResourceCompat:import android.support.v4.content.res.ResourcesCompat;ResourcesCompat.getDrawable(getResources(), R.drawable.name_of_drawable, null);
總結(jié)
以上是生活随笔為你收集整理的如何在android中设置背景,如何在Android中以编程方式设置背景可绘制的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android系统自动构建,[系统集成]
- 下一篇: 77城房价过万 深圳北京上海前三名