一共有两种 Log.e("drawable的id", R.drawable.ic_launcher + ""); // 包名 + : + 资源文件夹名 + /...+资源名 id = getResources().getIdentifier( "com.example.androidtest:drawable/ic_launcher...", null, null); Log.e("drawable的id", id + ""); // 第一个参数为ID名,第二个为资源属性是ID或者是Drawable,第三个为包名...id = getResources().getIdentifier("ic_launcher", "drawable", "com.example.androidtest...也可以用getIdentifier获取了id以后再findViewById,所以我做了测试 id = mSv.getContext().getResources()
痛点 但是,有时候也会有一些问题,比如我们根据服务器端的值取图片,但是服务器端绝对不会返回给我们的是资源id,最多是一种和文件名相关联的值,操作资源少的时候,可以维护一个容器进行值与资源ID的映射,但是多的话...几个简单的示例 lineos:false 1 2 3 4 5 6 7 8 9 10 11 12 13 Resources res = getResources(); final String packageName...= getPackageName(); int imageResId = res.getIdentifier("ic_launcher", "drawable", packageName); int...imageResIdByAnotherForm = res.getIdentifier(packageName + ":drawable/ic_launcher", null, null); int...musicResId = res.getIdentifier("test", "raw", packageName); int notFoundResId = res.getIdentifier
Imageview.setBackgroundResource(getResources().getIdentifier("add","drawable",getPackageName())); 但是对于以上做法..., 官方并不推荐,并且 getIdentifier这个写library的时候, 如果里面需要引用主程的资源且R所在的包名未知时还是很有用的(如果在gradle里修改了packageName, R所在的包名是和...packageName不一样的,没法反射),当然也是有解决的办法: 如果愿意,是可以根据业务拼接出 “package_name.R.drawable.class” 的。...(Exception e) { e.printStackTrace(); return -1; } } int id = getResId("icon", R.drawable.class
().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) {...----------2015-01-29 更新-------------------------------------- 公司有测试人员发现,FlymeOS4.x/Android4.4.4出现 了问题...,让人一头雾水,因此,这里把问题再次改正一下,目前Android4.4.4的系统不多,只检测到魅族的FlymeOS4.x有问题,所以对FlymeOS着重处理一下。.../ public void setTintDrawable(Drawable drawable) { setStatusBarTintDrawable(drawable);...(); int resourceId = res.getIdentifier(SHOW_NAV_BAR_RES_NAME, "bool", "android");
这个在同一个app下没有任何问题,但是在皮肤包中我们怎么获取这个id值呢。...这里我们需要注意的一点是getIdentifier(String name, String defType, String defPackage)方法和getString(int id)方法所调用Resources...drawable = resources.getDrawable(resources.getIdentifier("night_icon", "drawable","com.tzx.skin"));.../** * 获取Color资源 */ int color = resources.getColor(resources.getIdentifier("night_color...","color","com.tzx.skin")); imageView.setImageDrawable(drawable); textView.setText(text); }
去除无用资源 Link 检查(谨慎删除) 概念 Lint 是 Android Studio 提供的 代码扫描分析工具,它可以帮助我们发现代码结构 / 质量问题,同时提供一些解决方案,而且这个过程不需要我们手写测试用例...//动态获取资源 id , 未直接使用 R.xx.xx ,则这个 id 代表的资源会被认为没有使用过(类似不能混淆反射类) int indetifier =getResources().getIdentifier...("img_bubble_receive", "drawable", getPackageName()); getResources().getDrawable(indetifier); 5....不过,如果您的代码调用 Resources.getIdentifier()(或您的任何库进行了这一调用 - AppCompat 库会执行该调用),这就表示您的代码将根据动态生成的字符串查询资源名称。...String name = String.format("img_%1d", angle + 1); res = getResources().getIdentifier(name, "drawable
} String resName = dexResources.getResourceEntryName(resId); int outResId = dexResources.getIdentifier...} String resName = dexResources.getResourceEntryName(resId); int outResId = dexResources.getIdentifier...} String resName = dexResources.getResourceEntryName(resId); int outResId = dexResources.getIdentifier...); showTv.setText(getResources().getString(R.string.test_demo)); showTv.setTextColor(getResources...().getColor(R.color.yellow)); showTv.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg
(若只知道图片的名称,还可以通过图片的名称获得图片的id) (1)通过图片id获得Drawable Resources res = context.getResources(); Drawable drawable...= res.getDrawable(R.drawable.xxx); (2)通过图片id获得Bitmap Resources res = context.getResources(); Bitmap...(); int id =res.getIdentifier(name, defType, defPackage); //name:图片的名,defType:资源类型(drawable,string。。...大部分人都把的图片放在drawable—mdpi中。 在分辨率低于480*800时把图片放在drawable—mdpi中是不会有什么影响,但是当分辨率为420*800或高于它时就会出问题了。...你的手机屏幕有那么大但是他会将图片拉伸,当加载图片后让你感觉该屏幕没有实际的大小,而如果将图片放到drawable—hdpi中则该问题就不会存在了。
(); int resourceId = res.getIdentifier(SHOW_NAV_BAR_RES_NAME, "bool", "android"); ...getInternalDimensionSize(Resources res, String key) { int result = 0; int resourceId = res.getIdentifier...PS:在使用过程中发现了一些问题,使用以上方法对单个Activity有效,但是对继承了TabActivity的导航页怎么办呢?...Tab3Activity是三个子项,那么设置状态栏的代码需写在MainActivity中,而 android:fitsSystemWindows="true"需写在三个子Activity的xml布局文件中,这样设置后仍然有问题...,就是进入应用后首页也就是Tab1Activity没有问题,而Tab2Activity、Tab3Activity却没达到效果,它们的效果相当于未加android:fitsSystemWindows="true
public void setTintDrawable(Drawable drawable) { setStatusBarTintDrawable(drawable);...) private int getNavigationBarHeight(Context context) { Resources res = context.getResources...14) private int getNavigationBarWidth(Context context) { Resources res = context.getResources...(); int resourceId = res.getIdentifier(SHOW_NAV_BAR_RES_NAME, "bool", "android");...getInternalDimensionSize(Resources res, String key) { int result = 0; int resourceId = res.getIdentifier
有用点的知识点 1.动态图片设置 int resID = getResources().getIdentifier("p" + data[i], "drawable","com.z"); cards[
动态获取Drawable核心代码 public static ListDrawable> getImageResourable(Context context, String imgName) {...ListDrawable> imgList = new ArrayList(); Resources resources = context.getResources();...name = fields[i].getName(); if (name.contains(imgName)) { int resId = resources.getIdentifier...(name, "drawable", packagename); Drawable drawable = resources.getDrawable(resId);...03 在Drawable中添加图片 将我们准备好的图片一张张的复制进Drawable中,重命名为img开头的,如下图 ? 04 MainActivity的实现 ? 定义参数 ?
注意 : 这种方法格式化之后的浮点数对象是字符串形式 , 如果之后需要使用这个浮点数进行计算 , 就需要使用BigDecimal进行实现的这种方法 ; 精确度问题 : DecimalFormat使用的是...Resources resources = this.getResources(); int imageIndentify = resources.getIdentifier(imageName, "...drawable","chao.yun.demo"); 使用上面的代码可以通过字符串拼接图片名称 , 根据传入的参数 , 拼接imageName字符串 , 从而动态的获取图片对应的id; resources.getIdentifier...(); String imageName = "image_" + 1; int imageIndentify = resources.getIdentifier(imageName, "drawable...image_" + 3; imageIndentify = resources.getIdentifier(imageName, "drawable","chao.yun.demo"); ll_3.
="@drawable/a0" android:duration="100"/> drawable="@drawable/a1" android:duration=..."100"/> drawable="@drawable/a2" android:duration="100"/> drawable...="@drawable/a3" android:duration="100"/> drawable="@drawable/a4" android:duration=...animationDrawable = new AnimationDrawable(); for (int i = 0; i <= 25; i++) { int id = getResources...().getIdentifier("a" + i, "drawable", getPackageName()); Drawable drawable = getResources
="20dp" android:layout_marginTop="20dp" android:padding="5dp" android:src="@drawable...View statusBarView = new View(getContext()); statusBarView.setBackgroundColor(getContext().getResources...editor.commit(); } public int getStatusBarHeight() { int result = 0; int resourceId = getResources...().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) {...result = getResources().getDimensionPixelSize(resourceId); } return result; } }
()) { mResources = new VectorEnabledTintResources(this, super.getResources()); } return...super.getResources() : mResources; } 在 AppcompatActivity 中,有一个 getResources 方法用来获取 Resource。...assetManager, superRes.displayMetrics, superRes.configuration) //1,资源名称,资源类型,包名 val drawableId = resources.getIdentifier...("image_src", "drawable", "com.qs.redskin") //获取 资源 val drawable = resources.getDrawable(drawableId)...mImageView.setImageDrawable(drawable) 通过上面几步就可以加载 皮肤中的 资源文件了。
首先需要搞清楚 Resource 是怎样实例化的 @Override public Resources getResources() { return getResourcesInternal(...//返回应用程序包的Resources实例 public abstract Resources getResources(); 他的实现类其实就是 ContextImpl ,这个类在 as 上面是看不到的...assetManager, superRes.displayMetrics, superRes.configuration) //1,资源名称,资源类型,包名 val drawableId = resources.getIdentifier...("image_src", "drawable", "com.qs.redskin") //获取 资源 val drawable = resources.getDrawable(drawableId)...mImageView.setImageDrawable(drawable) 复制代码 通过上面几步就可以加载 皮肤中的 资源文件了。
drawable="@drawable/a8" android:duration="100"/> drawable="@drawable/...drawable="@drawable/a11" android:duration="100"/> drawable="@drawable...drawable="@drawable/a14" android:duration="100"/> drawable="@drawable...animationDrawable = new AnimationDrawable(); for (int i = 0; i <= 25; i++) { int id = getResources...().getIdentifier("a" + i, "drawable", getPackageName()); Drawable drawable = getResources
; import android.graphics.drawable.LayerDrawable; import android.graphics.drawable.RotateDrawable; import...); break; case GRAVITY_TOP: ivTriangle.setBackgroundResource(R.drawable.triangle_top); break...; } //对话框 主题长条的 View llContent.setBackgroundResource(R.drawable.round_corner_bg); // 如果用户调用...().getIdentifier( "status_bar_height", "dimen", "android"); if (resourceId > 0) { result = context.getResources...().getIdentifier("status_bar_height", "dimen", "android"); if (resourceId > 0) {
void generateVideoFrame() { // 从资源加载虚拟视频帧 Bitmap frame = BitmapFactory.decodeResource(getResources...(), getResources().getIdentifier("virtual_frame_" + (frameCounter % 10), "drawable