gpt4 book ai didi

java - 从数组android studio动态生成按钮

转载 作者:行者123 更新时间:2023-12-01 19:39:28 24 4
gpt4 key购买 nike

我正在尝试编写一种方法,该方法使用从名称和 ID 的二维数组获取的数据生成按钮,如果运行两次,该函数应该在已生成的按钮之后添加更多按钮。这些按钮将添加到约束布局(使用作为 Activity XML 一部分的准则 18 和 19)。

我正在使用全局按钮列表来存储已生成的按钮

List<Button> contactButtonList = new ArrayList<>();

然后这是我编写的用于生成按钮并将它们附加到引导线的函数。

 void updateButtonList(String[][] contactsArray){

//gets constraint layout
ConstraintLayout layout = findViewById(R.id.mainConstraint);
//creates constraint set
ConstraintSet set = new ConstraintSet();

//loops through array
for(int i = 0; i < contactsArray.length; i++){

//creates string to display on button
String buttonText = contactsArray[i][0]+": "+contactsArray[i][1];
//creates new button and sets ID based of size of list
Button btn = new Button(this);
btn.setId(contactButtonList.size());
//sets button text from array
btn.setText(buttonText);

//adds the button to the layout
layout.addView(btn,contactButtonList.size());

//sets the constraint set to match the current layout.... i think?, this needs to be done after adding the view
set.clone(layout);

//connects button to left vert constraint
set.connect(btn.getId(), ConstraintSet.LEFT, R.id.guideline19, ConstraintSet.RIGHT, 8);


//if first button attaches to horz constraint else attaches to last button
if(contactButtonList.isEmpty()){
set.connect(btn.getId(), ConstraintSet.TOP, R.id.guideline18, ConstraintSet.BOTTOM, 8);
}else{
set.connect(btn.getId(), ConstraintSet.TOP, contactButtonList.get(contactButtonList.size() - 1).getId(), ConstraintSet.BOTTOM);
}

//adds button to list
contactButtonList.add(btn);
}

//apply set to layout
set.applyTo(layout);
}

抱歉,如果有点长,希望评论有所帮助。

无论如何,问题是只有数组中的最后一个按钮附加到指南,另外 2 个按钮仅出现在左上角。

例如使用此数组时。

 String[][] testArray = {{"a","123"},{"bill","245"},{"test","246"}};

输出看起来像(请忽略 TextView 和紫色按钮)。 result

当所有按钮都应该出现在彼此下方时。

我假设第一个按钮隐藏在左上角第二个按钮的后面。

最佳答案

找到了在 for 循环内移动 set.applyTo(layout) 的解决方案,我还将 btn.setId(contactButtonList.size()); 更改为 btn.setId(View.generateViewId()); 但我不确定这是否有助于解决问题。这是工作代码(注意我还添加了一些功能,但这些功能可以忽略)。

void updateButtonList(final String[][] contactsArray){

//gets constraint layout
ConstraintLayout layout = findViewById(R.id.mainConstraint);
//creates constraint set
ConstraintSet set = new ConstraintSet();

//loops through array
for(int i = 0; i < contactsArray.length; i++){

//creates string to display on button
String buttonText = contactsArray[i][0]+": "+contactsArray[i][1];
//creates new button and sets ID based of size of list
Button btn = new Button(this);

//sets layout params width as 0 so we can set to match constraint
btn.setLayoutParams(new ConstraintLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT));

btn.setId(View.generateViewId());
//sets button text from array
btn.setText(buttonText);

//sets color of button based of pos in list (red for even blue for odd)
if((contactButtonList.size()%2) == 0) {
btn.setBackgroundResource(R.drawable.red_button);
}else{
btn.setBackgroundResource(R.drawable.blue_button);
}

//adds the button to the layout
layout.addView(btn,contactButtonList.size());

//sets the constraint set to match the current layout.... i think?, this needs to be done after adding the view
set.clone(layout);

//sets button width to match constraint
set.constrainDefaultWidth(btn.getId(), ConstraintSet.MATCH_CONSTRAINT_SPREAD);

//connects button to left vert constraint
set.connect(btn.getId(), ConstraintSet.LEFT, R.id.guideline19, ConstraintSet.RIGHT, 8);
//connects button to right ver constraint
set.connect(btn.getId(), ConstraintSet.RIGHT, R.id.guideline20, ConstraintSet.RIGHT, 8);

//if first button attaches to horz constraint else attaches to last button
if(contactButtonList.isEmpty()){
set.connect(btn.getId(), ConstraintSet.TOP, R.id.guideline18, ConstraintSet.BOTTOM, 8);
}else{
set.connect(btn.getId(), ConstraintSet.TOP, contactButtonList.get(contactButtonList.size() - 1).getId(), ConstraintSet.BOTTOM);
}

//adds button to list
contactButtonList.add(btn);
//adds id to list
contactIdList.add(contactsArray[i][1]);

//apply set to layout
set.applyTo(layout);
}


}

关于java - 从数组android studio动态生成按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59184537/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com