将可绘制图像添加到按钮

时间:2014-02-03 07:55:54

标签: android imagebutton android-drawable

我想将图像插入动态按钮,我可以获得除图像之外的所有内容。我希望我的按钮显示为文本+图像,但我得到文本+ 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);         

2 个答案:

答案 0 :(得分:1)

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

您正在尝试打印image这是一个可绘制的对象,因此它只会在您看到androidgraphics.drawable.bitmapdrawable@416e5

时显示地址信息

试      OutputData = Name;

您正在为按钮设置背景

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

答案 1 :(得分:0)

你应该做以下事情:

OutputData = Name; 

然后将文本添加到按钮执行以下操作:

button.setText(OutputData);

并将图像添加到按钮,执行以下操作:

   btn.setBackgroundResource(resId);

并给我一些反馈

希望有所帮助。