首先,我有一个创建视图的方法(是的,我知道它的错误,但它对我来说很好):
public ImageView createPhotoField(final Context context,
LinearLayout reviewsLayout, Integer id, String comment, OnClickListener a, OnClickListener b, int tag) {
/* reviewLayout */
LinearLayout reviewLayout = new LinearLayout(context);
reviewLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, densityToPixels(132,
context)));
reviewLayout.setOrientation(LinearLayout.VERTICAL);
reviewLayout.setGravity(Gravity.CENTER_VERTICAL);
/* reviewEntryTopLayout */
LinearLayout reviewEntryTopLayout = new LinearLayout(context);
reviewEntryTopLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, densityToPixels(65,
context)));
reviewEntryTopLayout.setOrientation(LinearLayout.HORIZONTAL);
reviewEntryTopLayout.setGravity(Gravity.CENTER_VERTICAL);
/* reviewsLayout */
reviewsLayout.addView(reviewLayout);
/* entryNumber */
ImageView imageview = new ImageView(context);
LinearLayout.LayoutParams entryNumberParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
imageview.setTag(tag);
entryNumberParams.setMargins(40, 0, 0, 0);
imageview.setLayoutParams(entryNumberParams);
imageview.setBackgroundResource(R.drawable.ic_bad);
imageview.setImageResource(R.drawable.ic_bad);
//entryNumber.set
imageview.setVisibility(View.GONE);
reviewLayout.addView(reviewEntryTopLayout);
//reviewEntryTopLayout.addView(entryNumber);
/* table layout */
TableLayout tl = new TableLayout(context);
tl.setLayoutParams(new TableLayout.LayoutParams(
TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.WRAP_CONTENT, 1.0f));
tl.setPadding(densityToPixels(20, context), 0, 0, 0);
reviewEntryTopLayout.addView(tl);
/* tableRow1 */
TableRow tableRow1 = new TableRow(context);
tableRow1.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT));
tl.addView(tableRow1);
/* textView1 */
TextView comment_photo = new TextView(context);
comment_photo.setLayoutParams(new TableRow.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
comment_photo.setText(comment);
tableRow1.addView(imageview);
tableRow1.addView(comment_photo);
/* lightSeperatorLayout */
LinearLayout lightSeperatorLayout = new LinearLayout(context);
lightSeperatorLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, densityToPixels(1,
context)));
lightSeperatorLayout.setOrientation(LinearLayout.VERTICAL);
lightSeperatorLayout.setBackgroundColor(Color.parseColor("#d5d5d5"));
/* reviewEntryBottomLayout */
LinearLayout reviewEntryBottomLayout = new LinearLayout(context);
reviewEntryBottomLayout.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, densityToPixels(65,
context)));
reviewEntryBottomLayout.setOrientation(LinearLayout.VERTICAL);
reviewEntryBottomLayout.setGravity(Gravity.CENTER_VERTICAL);
reviewLayout.addView(reviewEntryBottomLayout);
/* tableRow3 */
TableRow tableRow3 = new TableRow(context);
tableRow3.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT,
TableRow.LayoutParams.WRAP_CONTENT));
tableRow3.setPadding(densityToPixels(10, context),
densityToPixels(10, context), densityToPixels(10, context),
densityToPixels(10, context));
reviewEntryBottomLayout.addView(tableRow3);
/* editButton */
Button editButton = new Button(context);
TableRow.LayoutParams editButtonParams = new TableRow.LayoutParams(
Tabl
eRow.LayoutParams.WRAP_CONTENT, 150);
editButtonParams.weight = 0.5f;
editButtonParams.setMargins(15, 0, 30, 0);
editButton.setLayoutParams(editButtonParams);
editButton.setText("Take a photo");
tableRow3.addView(editButton);
/* reviewButton */
Button reviewButton = new Button(context);
TableRow.LayoutParams reviewButtonParams = new TableRow.LayoutParams(
TableRow.LayoutParams.WRAP_CONTENT, 150);
reviewButtonParams.weight = 0.5f;
reviewButtonParams.setMargins(30, 0, 15, 0);
reviewButton.setLayoutParams(reviewButtonParams);
reviewButton.setText("Gallery");
tableRow3.addView(reviewButto
n);
reviewLayout.addView(lightSeperatorLayout);
editButton.setOnClickListener(a);
reviewButton.setOnClickListener(b);
return imageview;
}
长话短说:此方法可创建:图片视图,照片,文字字段和两个按钮,可选择拍摄照片,或从画廊中拍摄。
在以下几次调用此方法后,将创建视图
image = element.createPhotoField(this, reviewsLayout,
Integer.parseInt(element_id), input_name, a, b,tag);
问题是:我找不到将照片放在正确位置的方法。我听说过标签,但我不知道如何实现这些标签。有经验的人可以帮我一把吗?
也许我应该使用适配器?
答案 0 :(得分:0)
在您的适配器中使用类似的东西来满足您的需求:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view=convertView;
ViewHolder holder;
if(convertView==null){
view = inflater.inflate(R.layout.youListItem, null);
holder = new ViewHolder();
holder.image=(ImageView)view.findViewById(R.id.youImageView);
view.setTag(holder);
}
else
holder=(ViewHolder)view.getTag();
holder.image.setTag(data[position]);
//to display image
holder.image.setImageDrawable(YourImageDrawable);
return view;
}
public static class ViewHolder{
//view elements of single list item
public ImageView image;
}