[英]android load tab icon from internal memory
如果需要选择,我需要更改标签图标。 我正在使用以下选择器,如果我使用可绘制文件夹中的图像,该选择器将起作用。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/search_selected"
android:state_selected="true" />
<item android:drawable="@drawable/search_normal" />
</selector>
但是,我需要使用数据/数据中的图像...,并使用以下代码从那里加载图像。 如果需要从内部存储器加载选择器,以加载正确的图像(无论是否选择)如何处理选择器? 谢谢
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
File imgFile = new File(pathImage);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
icon.setImageBitmap(myBitmap); }
//icon.setImageResource(R.drawable.tab_search;
使用StateListDrawables
。
将两个图像都获取为BitmapDrawable
并执行以下操作:
StateListDrawable selector = new StateListDrawable();
selector.addState(new int[]{ android.R.attr.state_enabled }, yourDefaultBitmapDrawable);
selector.addState(new int[]{ android.R.attr.state_selected }, yourSelectedBitmapDrawable);
icon.setImageDrawable(selector);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.