向 RelativeLayout 动态添加按钮无法正常工作
Dynamically adding a button to RelativeLayout not working correctly
我的目标是根据数据库中 "items" 的数量向 RelativeLayout 添加一些按钮(在 4 列网格中)。当我第一次学习如何向 RelativeLayout 添加按钮时,我刚刚创建了 6 个静态按钮并按以下方式添加它们(ItemButton 很简单 class,它扩展了 Button):
private void loadItemButtons2(){
itemButtonLayout = (RelativeLayout)findViewById(R.id.itemButtonLayout);
itemButtonLayout.removeAllViews();
ArrayList<Item> items = db.getAllActiveItems();
ItemButton b1, b2, b3, b4, b5, b6;
b1 = new ItemButton(this, items.get(0));
b2 = new ItemButton(this, items.get(1));
b3 = new ItemButton(this, items.get(2));
b4 = new ItemButton(this, items.get(3));
b5 = new ItemButton(this, items.get(4));
b6 = new ItemButton(this, items.get(5));
RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)b1.getLayoutParams();
params1.addRule(RelativeLayout.ALIGN_PARENT_START);
b1.setId(111);
b1.setLayoutParams(params1);
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams)b2.getLayoutParams();
params2.addRule(RelativeLayout.RIGHT_OF, 111);
b2.setId(222);
b2.setLayoutParams(params2);
RelativeLayout.LayoutParams params3 = (RelativeLayout.LayoutParams)b3.getLayoutParams();
params3.addRule(RelativeLayout.RIGHT_OF, 222);
b3.setId(333);
b3.setLayoutParams(params3);
RelativeLayout.LayoutParams params4 = (RelativeLayout.LayoutParams)b4.getLayoutParams();
params4.addRule(RelativeLayout.RIGHT_OF, 333);
b4.setId(444);
b4.setLayoutParams(params4);
RelativeLayout.LayoutParams params5 = (RelativeLayout.LayoutParams)b5.getLayoutParams();
params5.addRule(RelativeLayout.ALIGN_PARENT_START);
params5.addRule(RelativeLayout.BELOW, 111);
b5.setId(555);
b5.setLayoutParams(params5);
RelativeLayout.LayoutParams params6 = (RelativeLayout.LayoutParams)b6.getLayoutParams();
params6.addRule(RelativeLayout.RIGHT_OF, 555);
params6.addRule(RelativeLayout.BELOW, 222);
b6.setId(666);
b6.setLayoutParams(params6);
itemButtonLayout.addView(b1);
itemButtonLayout.addView(b2);
itemButtonLayout.addView(b3);
itemButtonLayout.addView(b4);
itemButtonLayout.addView(b5);
itemButtonLayout.addView(b6);
}
这给了我一个完美的结果:
但这是我想出的动态解决方案,对我来说,它似乎在做完全相同的事情,但结果却非常不稳定:
private void loadItemButtons(){
itemButtonLayout = (RelativeLayout)findViewById(R.id.itemButtonLayout);
itemButtonLayout.removeAllViews();
ArrayList<Item> items = db.getAllActiveItems();
int colCount = 0;
int rowCount = 0;
int i = 0;
while(i < items.size()-1){
ItemButton newItemButton = new ItemButton(this, items.get(i));
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)newItemButton.getLayoutParams();
newItemButton.setId(i);
if(colCount == 0){
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
}else if(colCount == 1){
layoutParams.addRule(RelativeLayout.RIGHT_OF, i-1);
}else if(colCount == 2){
layoutParams.addRule(RelativeLayout.RIGHT_OF, i-1);
}else if(colCount == 3){
layoutParams.addRule(RelativeLayout.RIGHT_OF, i-1);
}
//If we are in any row except the top row, place in reference to the button above it
if(rowCount != 0){
layoutParams.addRule(RelativeLayout.BELOW, i-4);
}
newItemButton.setLayoutParams(layoutParams);
itemButtonLayout.addView(newItemButton);
if(colCount == 3){
colCount = 0;
rowCount += 1;
}else{
colCount += 1;
}
i++;
}
}
有人能看出我做错了什么或与第一个例子不同吗???非常感谢任何建议!
我没有测试代码:
private void loadItemButtons(){
itemButtonLayout = (RelativeLayout)findViewById(R.id.itemButtonLayout);
itemButtonLayout.removeAllViews();
ArrayList<Item> items = db.getAllActiveItems();
int colCount = 0;
int rowCount = 0;
int i = 0;
while(i < items.size())
{
ItemButton newItemButton = new ItemButton(this, items.get(i));
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)newItemButton.getLayoutParams();
newItemButton.setId(i);
if(colCount == 0)
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
else
layoutParams.addRule(RelativeLayout.RIGHT_OF, i-1);
//If we are in any row except the top row, place in reference to the button above it
if(rowCount != 0){
layoutParams.addRule(RelativeLayout.BELOW, i-4);
}
itemButtonLayout.addView(newItemButton, i, layoutParams);
if(colCount == 3){
colCount = 0;
rowCount += 1;
}else{
colCount += 1;
}
i++;
}
}
您失败的原因是您使用的 ID。 Android 使用 "reserved" id 来表示应用的一般内容区域。
使用您的代码,我能够将 1000 添加到每个 ID 并生成预期结果。
请注意我下面的清理工作:
private void loadItemButtons(){
itemButtonLayout = (RelativeLayout)findViewById(R.id.itemButtonLayout);
itemButtonLayout.removeAllViews();
List<String> items = itemList;
int colCount = 0;
int rowCount = 0;
// # of items per column
int colSpan = 4;
final int itemListSize = itemList.size();
for (int i = 0; i < itemListSize; i++) {
int id = 1000 + i;
ItemButton newItemButton = new ItemButton(this, items.get(i));
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
newItemButton.setId(id);
if(colCount == 0)
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
else
layoutParams.addRule(RelativeLayout.RIGHT_OF, id-1);
//If we are in any row except the top row, place in reference to the button above it
if(rowCount != 0)
layoutParams.addRule(RelativeLayout.BELOW, id-colSpan);
newItemButton.setLayoutParams(layoutParams);
itemButtonLayout.addView(newItemButton);
if(colCount == colSpan - 1)
rowCount += 1;
colCount = (colCount + 1) % colSpan;
}
}
此外,正如我在对原始 post 的评论中提到的,您确实应该为此使用 gridview,这就是它的用途。
我可以用干草叉挖一个洞,但我敢打赌铲子会更好。
我的目标是根据数据库中 "items" 的数量向 RelativeLayout 添加一些按钮(在 4 列网格中)。当我第一次学习如何向 RelativeLayout 添加按钮时,我刚刚创建了 6 个静态按钮并按以下方式添加它们(ItemButton 很简单 class,它扩展了 Button):
private void loadItemButtons2(){
itemButtonLayout = (RelativeLayout)findViewById(R.id.itemButtonLayout);
itemButtonLayout.removeAllViews();
ArrayList<Item> items = db.getAllActiveItems();
ItemButton b1, b2, b3, b4, b5, b6;
b1 = new ItemButton(this, items.get(0));
b2 = new ItemButton(this, items.get(1));
b3 = new ItemButton(this, items.get(2));
b4 = new ItemButton(this, items.get(3));
b5 = new ItemButton(this, items.get(4));
b6 = new ItemButton(this, items.get(5));
RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams)b1.getLayoutParams();
params1.addRule(RelativeLayout.ALIGN_PARENT_START);
b1.setId(111);
b1.setLayoutParams(params1);
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams)b2.getLayoutParams();
params2.addRule(RelativeLayout.RIGHT_OF, 111);
b2.setId(222);
b2.setLayoutParams(params2);
RelativeLayout.LayoutParams params3 = (RelativeLayout.LayoutParams)b3.getLayoutParams();
params3.addRule(RelativeLayout.RIGHT_OF, 222);
b3.setId(333);
b3.setLayoutParams(params3);
RelativeLayout.LayoutParams params4 = (RelativeLayout.LayoutParams)b4.getLayoutParams();
params4.addRule(RelativeLayout.RIGHT_OF, 333);
b4.setId(444);
b4.setLayoutParams(params4);
RelativeLayout.LayoutParams params5 = (RelativeLayout.LayoutParams)b5.getLayoutParams();
params5.addRule(RelativeLayout.ALIGN_PARENT_START);
params5.addRule(RelativeLayout.BELOW, 111);
b5.setId(555);
b5.setLayoutParams(params5);
RelativeLayout.LayoutParams params6 = (RelativeLayout.LayoutParams)b6.getLayoutParams();
params6.addRule(RelativeLayout.RIGHT_OF, 555);
params6.addRule(RelativeLayout.BELOW, 222);
b6.setId(666);
b6.setLayoutParams(params6);
itemButtonLayout.addView(b1);
itemButtonLayout.addView(b2);
itemButtonLayout.addView(b3);
itemButtonLayout.addView(b4);
itemButtonLayout.addView(b5);
itemButtonLayout.addView(b6);
}
这给了我一个完美的结果:
但这是我想出的动态解决方案,对我来说,它似乎在做完全相同的事情,但结果却非常不稳定:
private void loadItemButtons(){
itemButtonLayout = (RelativeLayout)findViewById(R.id.itemButtonLayout);
itemButtonLayout.removeAllViews();
ArrayList<Item> items = db.getAllActiveItems();
int colCount = 0;
int rowCount = 0;
int i = 0;
while(i < items.size()-1){
ItemButton newItemButton = new ItemButton(this, items.get(i));
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)newItemButton.getLayoutParams();
newItemButton.setId(i);
if(colCount == 0){
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
}else if(colCount == 1){
layoutParams.addRule(RelativeLayout.RIGHT_OF, i-1);
}else if(colCount == 2){
layoutParams.addRule(RelativeLayout.RIGHT_OF, i-1);
}else if(colCount == 3){
layoutParams.addRule(RelativeLayout.RIGHT_OF, i-1);
}
//If we are in any row except the top row, place in reference to the button above it
if(rowCount != 0){
layoutParams.addRule(RelativeLayout.BELOW, i-4);
}
newItemButton.setLayoutParams(layoutParams);
itemButtonLayout.addView(newItemButton);
if(colCount == 3){
colCount = 0;
rowCount += 1;
}else{
colCount += 1;
}
i++;
}
}
有人能看出我做错了什么或与第一个例子不同吗???非常感谢任何建议!
我没有测试代码:
private void loadItemButtons(){
itemButtonLayout = (RelativeLayout)findViewById(R.id.itemButtonLayout);
itemButtonLayout.removeAllViews();
ArrayList<Item> items = db.getAllActiveItems();
int colCount = 0;
int rowCount = 0;
int i = 0;
while(i < items.size())
{
ItemButton newItemButton = new ItemButton(this, items.get(i));
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)newItemButton.getLayoutParams();
newItemButton.setId(i);
if(colCount == 0)
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
else
layoutParams.addRule(RelativeLayout.RIGHT_OF, i-1);
//If we are in any row except the top row, place in reference to the button above it
if(rowCount != 0){
layoutParams.addRule(RelativeLayout.BELOW, i-4);
}
itemButtonLayout.addView(newItemButton, i, layoutParams);
if(colCount == 3){
colCount = 0;
rowCount += 1;
}else{
colCount += 1;
}
i++;
}
}
您失败的原因是您使用的 ID。 Android 使用 "reserved" id 来表示应用的一般内容区域。
使用您的代码,我能够将 1000 添加到每个 ID 并生成预期结果。
请注意我下面的清理工作:
private void loadItemButtons(){
itemButtonLayout = (RelativeLayout)findViewById(R.id.itemButtonLayout);
itemButtonLayout.removeAllViews();
List<String> items = itemList;
int colCount = 0;
int rowCount = 0;
// # of items per column
int colSpan = 4;
final int itemListSize = itemList.size();
for (int i = 0; i < itemListSize; i++) {
int id = 1000 + i;
ItemButton newItemButton = new ItemButton(this, items.get(i));
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
newItemButton.setId(id);
if(colCount == 0)
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START);
else
layoutParams.addRule(RelativeLayout.RIGHT_OF, id-1);
//If we are in any row except the top row, place in reference to the button above it
if(rowCount != 0)
layoutParams.addRule(RelativeLayout.BELOW, id-colSpan);
newItemButton.setLayoutParams(layoutParams);
itemButtonLayout.addView(newItemButton);
if(colCount == colSpan - 1)
rowCount += 1;
colCount = (colCount + 1) % colSpan;
}
}
此外,正如我在对原始 post 的评论中提到的,您确实应该为此使用 gridview,这就是它的用途。
我可以用干草叉挖一个洞,但我敢打赌铲子会更好。