动态元素数量布局问题

3

我正在尝试创建一个带有动态数量的按钮的活动(Activity),每个按钮代表一个Shadow对象。

我不确定是否可以在XML文件中完成,所以我决定使用Java代码。

显示所有List元素的整个想法是可行的——为列表中的每个Shadow生成了一个按钮。

我的问题是,这些按钮都在同一个位置上,最大的一个覆盖了其他按钮。如何使它们正确地显示,例如它们之间有16dp的垂直空间?

以下是类代码:

package com.jackw.nfshadow;

import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;

import com.biernacki.nfshadow.tagdiscoveredactivities.Shadow;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class ShadowLibrary extends AppCompatActivity {

    private List<Shadow> savedShadows;
    private ConstraintLayout layout;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if(savedInstanceState != null) {
            super.onCreate(savedInstanceState);
        }else{
            super.onCreate(new Bundle());
        }
        setContentView(R.layout.activity_shadow_library);
        layout  = findViewById(R.id.shadow_library);
        setButtonNames();
    }

    private void setButtonNames() {
        if(Main.savedShadows != null && Main.savedShadows.size() > 0){
            savedShadows = Main.savedShadows.stream()
                    .sorted(Comparator.comparing(Shadow::getName))
                    .collect(Collectors.toList());

            savedShadows.forEach(shadow -> {
                Button button = new Button(this);
                button.setId(View.generateViewId());
                button.setText(shadow.getName());
                layout.addView(button);
            });

        }
    }
}

因为我对Android完全是新手,所以我也希望了解有关创建动态内容/对我上面编写的代码进行小型评论的其他方法的任何信息。

更新: 我已经稍微编辑了setButtonNames(),仍然尝试使用ConstraintLayoutconnect()方法,但出于某种原因它仍然无法工作。我找不到这个问题在哪里。

savedShadows.forEach(shadow -> {
                Button button = new Button(this);
                button.setId(View.generateViewId());
                button.setText(shadow.getName());

                if(shadow == savedShadows.get(0)){
                    previousButtonId = button.getId();
                }else{
                    constraints.connect(button.getId(), ConstraintSet.TOP, previousButtonId, ConstraintSet.BOTTOM, 16);
                    previousButtonId = button.getId();
                }
                constraints.applyTo(layout);
                layout.addView(button);
            });
        }
}

2
尝试使用简单的LinearLayout而不是需要额外约束信息的ConstraintLayout - undefined
你找到解决方案了吗? - undefined
还没有。我正在尝试使用约束布局和约束条件,但是还有些问题。一旦我成功解决这个问题,我会更新帖子的内容。 - undefined
1个回答

1
你的问题是你没有为你的按钮添加约束条件。你可以像这个例子一样做,其中layout是你的ConstraintLayout
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.clone(layout);
constraintSet.connect(someButton.getId(), ConstraintSet.TOP, 
                       anotherButton.getId(), ConstraintSet.BOTTOM);
constraintSet.applyTo(constraintLayout);

你需要根据自己的需求添加约束条件。或者将你的ConstraintLayout替换为LinearLayout,这样它就会自动正确地添加按钮,无需使用约束条件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接