日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

如何在android中设置背景,如何在Android中以编程方式设置背景可绘制

發布時間:2024/9/27 Android 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何在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中以编程方式设置背景可绘制的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。