gpt4 book ai didi

android - 将可绘制图像添加到按钮

转载 作者:行者123 更新时间:2023-11-30 03:07:41 25 4
gpt4 key购买 nike

我想在动态按钮中插入图片,我可以获得除图片以外的所有内容。我希望我的按钮显示为文本 + 图像,但我得到的是文本 + androidgraphics.drawable.bitmapdrawable@416e5 等。任何人都可以告诉我代码中的错误是什么,感谢任何帮助。

private class LongOperation  extends AsyncTask<String, Void, Void> {
private final HttpClient Client = new DefaultHttpClient();
private String Content;
private String Error = null;
private ProgressDialog Dialog = new ProgressDialog(MainActivity.this);
String data ="";

int sizeData = 0;
private View addButton;
protected Bitmap myBitmap;
private String image;

protected void onPreExecute() {

Dialog.setMessage("Please wait..");
Dialog.show();

}

// Call after onPreExecute method
protected Void doInBackground(String... urls) {
/************ Make Post Call To Web Server ***********/
BufferedReader reader=null;
// Send data
try
{
// Defined URL where to send data
URL url = new URL(urls[0]);


JSONArray jsonMainNode1 = jsonResponse.getJSONArray("menu");
// JSONObject e = jsonMainNode1.getJSONObject(position);

int lengthJsonArr = jsonMainNode1.length();
for(int i=0; i <lengthJsonArr; i++)
{
JSONObject jsonChildNode = jsonMainNode1.getJSONObject(i);

String Pid = jsonChildNode.optString("pid".toString());
String Name = jsonChildNode.optString("name").toString();
String Refid=jsonChildNode.optString("refid".toString());


String imagefile = jsonChildNode.getString("image_url");
String resName = imagefile.split("\\.")[2];


int resId = getResources().getIdentifier(resName, "drawable", getPackageName());
Drawable image = getResources().getDrawable(resId);


OutputData = Name+image;

str_id = Pid;

LinearLayout buttonContainer=(LinearLayout)findViewById(R.id.btn_container);
Button button = new Button(buttonContainer.getContext());

button.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
ViewGroup.LayoutParams params = button.getLayoutParams();
//Button new width
params.width = 150;
params.height = 150;
button.setLayoutParams(params);
button.setText(OutputData);
// button.setBackground(itemImage);

button.setTag(Refid);
button.setTextColor(Color.parseColor("#FEFCFF"));
button.setBackgroundResource(R.drawable.button_color);


buttonContainer.addView(button);

最佳答案

Drawable image = getResources().getDrawable(resId);

您正在尝试打印可绘制对象image,因此它只会显示地址信息,如您所见androidgraphics.drawable.bitmapdrawable@416e5

尝试 OutputData = 名称;

你正在为按钮设置背景

button.setBackgroundResource(R.drawable.button_color); 您确定您期望的图像与 button_color 相同吗,听起来像一种颜色。

关于android - 将可绘制图像添加到按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21522209/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com