Android 以编程方式将可绘制图形设置为背景
编辑:很抱歉,我从你的评论中意识到我的问题不够清楚。我会发布一个新的。对此表示抱歉,并感谢您的回答 我正在从Json文件填充ListView 使用我的listadapter,我可以轻松地为列表的每一行分配适当的json数据。这适用于文本,例如:Android 以编程方式将可绘制图形设置为背景,android,json,drawable,Android,Json,Drawable,编辑:很抱歉,我从你的评论中意识到我的问题不够清楚。我会发布一个新的。对此表示抱歉,并感谢您的回答 我正在从Json文件填充ListView 使用我的listadapter,我可以轻松地为列表的每一行分配适当的json数据。这适用于文本,例如: TextView tv = (TextView)ll.findViewById(R.id.descView); tv.setText(i.desc); 使用上述代码,每一行都将正确地填充好的json数据 但是,我无法对图像执行相同的操作。我尝试使
TextView tv = (TextView)ll.findViewById(R.id.descView);
tv.setText(i.desc);
使用上述代码,每一行都将正确地填充好的json数据
但是,我无法对图像执行相同的操作。我尝试使用以下方法从json数据设置正确的图像:
ImageView iv = (ImageView)ll.findViewById(R.id.imgView);
iv.setBackgroundDrawable(context.getResources().getDrawable(i.img));
我猜我的参数类型有问题:“setBackgroundDrawable”需要一个drawable参数。
“getDrawable”需要一个int。
我已经将字段img的类型设置为int,但这不起作用
知道为什么吗
我的列表适配器:
public class adapter extends ArrayAdapter<ListItems> {
int resource;
String response;
Context context;
//Initialize adapter
public ListItemsAdapter(Context context, int resource, List<ListItems> items) {
super(context, resource, items);
this.resource=resource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
//Get the current object
ListItems i = getItem(position);
//Inflate the view
if(convertView==null)
{
ll = new LinearLayout(getContext());
String inflater = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li;
li = (LayoutInflater)getContext().getSystemService(inflater);
li.inflate(resource, ll, true);
}
else
{
ll = (LinearLayout) convertView;
}
//For the message
TextView tv = (TextView)ll.findViewById(R.id.descView);
tv.setText(i.desc);
// For the Img
ImageView iv = (ImageView)ll.findViewById(R.id.imgView);
iv.setBackgroundDrawable(context.getResources().getDrawable(i.img));
return ll;
}
public class ListItems{
int id;
int img;
String desc;}
以及我的json文件示例:
[{"id":10001,"img":e1,"desc":"desc1"},
{"id":10002,"img":e2,"desc":"desc2"},
{"id":10003,"img":e3,"desc":"desc3"}]
试试这个
iv.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.img));
或
现在,由于
getDrawable
和setBackgroundDrawable
都是去擦亮的
,您应该将drawable设置为如下背景:
view.setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable));
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.your_drawable));
} else {
view.setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable));
}
如果您的目标minSdk低于16,则进行如下检查:
view.setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable));
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(ContextCompat.getDrawable(this, R.drawable.your_drawable));
} else {
view.setBackground(ContextCompat.getDrawable(this, R.drawable.your_drawable));
}
if(android.os.Build.VERSION.SDK\u INT
这里是新方法
recyclerView.setBackgroundResource(R.drawable.edit\u text\u button\u shape)
别用这个,这是个老办法
recyclerView.setBackgroundDrawable(this.getResources().getDrawable(编辑文本按钮形状)) 您可能希望使用setImageResources,它将int作为参数并设置imageview的src。然而,这个问题与挫折无关。你的代码也不能正常工作。ll既不包含textview也不包含ImageView e1是什么意思?json数据中的图像在哪里?能否发布“资源”布局?e1、e2、e3要设置的可绘制图像的名称?应使用iv.setImageResource(R.drawable.img)@CorayThan如何在以前的API版本中实现这一点(@CorayThan您找到旧API的解决方案了吗?