我是Android的新手,任何人都可以告诉程序将背景图像添加到动态按钮。我的代码用于重新启动json并创建button.i添加了image_url是我的dadabase列,我添加了图像名称为imge.png。现在我正在获取图像字符串。我不知道如何检索图像。
JSONArray jsonMainNode1 = jsonResponse.getJSONArray("menu");
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 image = jsonChildNode.optString("image_url").toString();
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 = 64;
params.height = 64;
button.setLayoutParams(params);
button.setText(OutputData);
button.setTag(Refid);
button.setTextColor(Color.parseColor("#FEFCFF"));
button.setBackgroundResource(R.drawable.button_color);
button.setBackgroundDrawable(ImgDrawableFromFile(
getResources(), "/data/data/com.example.proj2/images"));
buttonContainer.addView(button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
LinearLayout buttonContainer = (LinearLayout)findViewById(R.id.btn_container);
buttonContainer.removeAllViews();
Toast.makeText(getApplicationContext(),
"button" +v.getTag()+ "is clicked", Toast.LENGTH_SHORT).show();
new LongOperation().execute("http://10.0.2.2:80/android_connect/home2.php?pid="+v.getTag());
}
public Drawable ImgDrawableFromFile(Resources res, String file_path) {
File imgFile = new File(file_path);
if (imgFile.exists()) {
myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
if (myBitmap != null)
return new BitmapDrawable(res, myBitmap);
else
return null;
}
return null;
}
}
);
}
}
}
//if closing bracket
catch (JSONException e) {
e.printStackTrace();
}
}
}
private Drawable ImgDrawableFromFile(Resources resources, String string) {
return null;
}
}
public void newclick(View v){
Button backbutt=(Button)findViewById(R.id.button1);
backbutt.setTag(str_id);
LinearLayout buttonContainer = (LinearLayout)findViewById(R.id.btn_container);
buttonContainer.removeAllViews();
new LongOperation().execute("http://10.0.2.2:80/android_connect/home2.php?rid="+ v.getTag());
{
Toast.makeText(getApplicationContext(),"button"+ v.getTag() + "is clicked",Toast.LENGTH_SHORT).show();
}
//LinearLayout back_button = (LinearLayout)findViewById(R.id.butt_layout);
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:1)
尝试这种方式:创建Drawable
,如下所示:
button.setBackgroundDrawable(ImgDrawableFromFile(
getResources(), your image path)
并从Storage
加载图片:
public Drawable ImgDrawableFromFile(Resources res, String file_path) {
File imgFile = new File(file_path);
if (imgFile.exists()) {
myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
if (myBitmap != null)
return new BitmapDrawable(res, myBitmap);
else
return null;
}
return null;
}
如果您要加载来自Assets
的图片,请在下方使用:
private Drawable getBitmapFromAsset(Resources res,String strName)
{
AssetManager assetManager = getAssets();
InputStream istr = null;
try {
istr = assetManager.open(strName);
} catch (IOException e) {
e.printStackTrace();
}
Bitmap bitmap = BitmapFactory.decodeStream(istr);
if (bitmap != null)
return new BitmapDrawable(res, bitmap);
else
return null;
}