java - 以编程方式在android中动态创建 View 对象

标签 java android

所以我有一个 Java 类,它创建动态相对布局并将其添加到主 Activity 的线性布局中。我的按钮在 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);
}

}

最佳答案

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

关于java - 以编程方式在android中动态创建 View 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25111394/

相关文章:

java - 寻求面向对象编程的建议

java - 过滤掉数字Java

java - Java 中的向后循环更快吗?

java - 如何在首选项之前显示加载屏幕类 [Libgdx & android]

java - Ibinder 和 JobintentService

java - 如何在构造函数中调用方法?

java - 使用 OpenCSV 查找列号

java - 添加 Firebase UI 依赖项时出现错误 :Execution failed for task ':app:processDebugManifest' .

Android:使用 Assets 文件时出现 ZipFile、FileNotFoundException

java - flutter 安卓错误: How to extends MainActivity from "Two Packages"