以编程方式在android中动态创建视图对象

时间:2014-08-04 03:30:02

标签: java android

所以我有一个创建动态相对布局的Java类,并将其添加到主活动的线性布局中。我的按钮在Android模拟器中显示在彼此的顶部,我不知道为什么。从我收集的研究结果来看,我做对了。

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);
}

}

1 个答案:

答案 0 :(得分:1)

为每个视图创建一个新的params对象。对所有这些使用相同的一个会导致您每次都更改每个视图的值,因为它们是通过引用存储的。