如何在android中设置背景,如何在Android中以编程方式设置背景可绘制
MMTTMM
layout.setBackgroundResource(R.drawable.ready);是正確的。實現它的另一種方法是使用以下方法: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));}但我認為問題出現是因為您正在嘗試加載大圖像。這是一個很好的教程如何加載大位圖。更新:API級別22中不推薦使用的getDrawable(int)?getDrawable(int )現在已在API級別22中棄用。您應該使用支持庫中的以下代碼:ContextCompat.getDrawable(context, R.drawable.ready)如果你引用ContextCompat.getDrawable的源代碼,它會給你這樣的東西:/**?* 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);? ? }}關于ContextCompat的更多細節?從API 22開始,您應該使用該getDrawable(int, Theme)方法而不是getDrawable(int)。更新:如果您使用的是支持v4庫,則以下內容對所有版本都足夠了。ContextCompat.getDrawable(context, R.drawable.ready)您需要在app build.gradle中添加以下內容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);
總結
以上是生活随笔為你收集整理的如何在android中设置背景,如何在Android中以编程方式设置背景可绘制的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: android系统自动构建,[系统集成]
- 下一篇: android token机制_对And