我在描述我的问题时引用了一些注释,但是注释在代码容器之外,您必须滚动才能看到注释。
因此,我试图从R.drawable获得一个可绘制的(图像),但遇到了一些问题。我从第1次尝试(检查代码注释)开始,并获得了所有bounds=0的图像。但是后来我读到这里,getDrawable()可能返回图像的错误缩放,所以我按照通知进行了尝试,并尝试了尝试2(再次检查代码注释),但是在本例中,可绘制值为null。
下面是代码:
private void setCurrentImage(){
TextView text = (TextView)profileView.findViewById(R.id.profile_image);
//mImage = "flower";
int id = R.drawable.flower;
if(mImage.equals("flower"))
id = R.drawable.flower;
if(mImage.equals("event_splash"))
id = R.drawable.event_splash;
if(mImage.equals("football"))
id = R.drawable.football;
if(mImage.equals("foxhead"))
id = R.drawable.foxhead;
if(mImage.equals("computer"))
id = R.drawable.computer;
//Drawable image = getResources().getDrawable(R.drawable.foxhead); //attempt 1
TypedArray attr = getActivity().obtainStyledAttributes(new int[]{id}); //attempt 2
Drawable image = attr.getDrawable(0); //attempt 2
Log.d("bounds", image.getBounds().toString());
text.setCompoundDrawables(image, null, null, null);
}
正如您在注释的行mImage =“花”中看到的那样,我100%确定mImage是有效的,但仍然没有工作。
发布于 2013-10-03 08:58:27
来自TextView文档:
公共无效setCompoundDrawables (可拉左、可拔上、可拉右、可拉底部) 将Drawable(如果有的话)设置为显示在文本的左边、上方、右侧和下面。如果您不希望那里有可绘制的,请使用null。Drawables夫妇一定已经被调用了setBounds(Rect)。
因此,一旦拉出可绘制的图形,就必须调用setBounds来指定要显示图像的边界。
https://stackoverflow.com/questions/19163794
复制相似问题