Java 在android中以编程方式动态创建视图对象
所以我有一个Java类,它创建了一个动态相对布局,并将其添加到主活动的线性布局中。我的按钮在android emulator中一个接一个地出现,我不知道为什么。从我搜集的资料来看,一切都是对的Java 在android中以编程方式动态创建视图对象,java,android,Java,Android,所以我有一个Java类,它创建了一个动态相对布局,并将其添加到主活动的线性布局中。我的按钮在android emulator中一个接一个地出现,我不知道为什么。从我搜集的资料来看,一切都是对的 public class Feed { private String status; private Uri image; private boolean imageContent; public Feed(String status) { this.status = status; i
public class Feed {
private String status;
private Uri image;
private boolean imageContent;
public Feed(String status) {
this.status = status;
imageContent = false;
}
public Feed(String status, Uri image) {
this.status = status;
this.image = image;
imageContent = true;
}
public void showFeed(Context page, LinearLayout main) {
RelativeLayout sample = new RelativeLayout(page);
sample.setBackgroundResource(R.drawable.sample);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
final Button like = new Button(page);
like.setId(1);
like.setText("like");
like.setTextColor(page.getResources().getColor(R.color.yellow));
like.setBackgroundColor(Color.TRANSPARENT);
sample.addView(like, params);
final Button comment = new Button(page);
comment.setId(2);
comment.setText("comment");
comment.setTextColor(page.getResources().getColor(R.color.yellow));
comment.setBackgroundColor(Color.TRANSPARENT);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ALIGN_RIGHT, like.getId());
comment.setLayoutParams(params);
sample.addView(comment);
final Button pin = new Button(page);
pin.setId(4);
pin.setText("pin");
pin.setTextColor(page.getResources().getColor(R.color.yellow));
pin.setBackgroundColor(Color.TRANSPARENT);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
sample.addView(pin, params);
final Button share = new Button(page);
share.setId(3);
share.setText("share");
share.setTextColor(page.getResources().getColor(R.color.yellow));
share.setBackgroundColor(Color.TRANSPARENT);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.ALIGN_LEFT, pin.getId());
params.setMargins(0, 10, 0, 0);
sample.addView(share, params);
TextView statusText = new TextView(page);
statusText.setId(5);
statusText.setTextColor(page.getResources().getColor(R.color.white));
statusText.setText(status);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_TOP, like.getId());
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
sample.addView(statusText, params);
if(imageContent) {
ImageView statusPhoto = new ImageView(page);
statusPhoto.setId(5);
statusPhoto.setImageURI(image);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_TOP, statusText.getId());
sample.addView(statusPhoto, params);
}
main.addView(sample, params);
}
}为每个视图创建一个新的params对象。对所有视图使用同一个会导致每次更改每个视图的值,因为它们是通过引用存储的。我创建了一个params1对象,设置了所有规则,并将其放在另一个按钮的顶部(类似按钮顶部的注释)。这很奇怪,因为当我没有设置任何新规则时,它会将其放在左上角。