0
我想在運行時創建一個佈局,然後我想在運行時使用循環添加一些imageview。但問題是我使用LayoutParams的餘量根本無法工作。我嘗試了很多,但沒有找到任何解決方案。我也附加我的代碼。如何在android中使用代碼在imageview上設置邊距?
RelativeLayout ll_main_image = new RelativeLayout(activity);
ll_main_image.setLayoutParams(new LayoutParams(
width,
width));
for (int i = 0; i < al.get(position).al.size(); i++) {
//creating layout for tags
ImageView view_tag = new ImageView(activity);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
int int_margin_left = (int)(((Double.parseDouble(al.get(position).al.get(i).str_x_pos))/100)*width);
int int_margin_top = (int)(((Double.parseDouble(al.get(position).al.get(i).str_y_pos))/100)*width);
System.out.println("margins perc " + al.get(position).al.get(i).str_x_pos + "/" + al.get(position).al.get(i).str_y_pos + "/" + width);
System.out.println("margins " + int_margin_left + "/" + int_margin_top + "/" + width);
lp.setMargins(int_margin_left, int_margin_top, 0, 0);
view_tag.setLayoutParams(lp);
view_tag.setBackgroundResource(R.drawable.tag_light);
ll_main_image.addView(view_tag);
}
完美,非常感謝。你救了我很多時間男人:) – rahul 2014-09-22 12:09:58