正如我在标题中所描述的,我在sqlite中有一个本地数据库。我想以参数方式创建ImageButton
。本地数据库循环执行了多少次?请参阅以下代码:
RelativeLayout outerRelativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout2_making_dynamically);
db.open();
Cursor c = db.getAllLocal_Job_Data();
if(c!=null){
if(c.moveToFirst()){
do {
RelativeLayout innerRelativeLayout = new RelativeLayout(CalendarActivity.this);
innerRelativeLayout.setGravity(Gravity.CENTER);
ImageButton imgBtn = new ImageButton(CalendarActivity.this);
imgBtn.setBackgroundResource(R.drawable.color_001);
RelativeLayout.LayoutParams imageViewParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
imgBtn.setLayoutParams(imageViewParams);
// Adding the textView to the inner RelativeLayout as a child
innerRelativeLayout.addView(imgBtn, new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
outerRelativeLayout.addView(innerRelativeLayout, new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
} while (c.moveToNext());
}
}
db.close();
但是当我运行项目时,我只能看到在那里创建了一个按钮。我认为有很多按钮,但这里的图像按钮是在最后创建的图像按钮上创建的。我想我应该使用android:layout_toRightOf
与之前创建的按钮,但我无法找到如何将它放在这里。我尝试了一些想法,但它没有改变任何事情。所以请任何人有任何想法解决我的问题然后请与我分享。
答案 0 :(得分:0)
您正在正确创建多个ImageButtons,但是您将它们添加到相同位置的相对布局中(具体而言,将内部相对布局添加到外部相对布局)。我不知道您要实现的布局,但您可以尝试将外部布局更改为LinearLayout。
此外,您并不真正需要内部布局 - 您可以将ImageButtons直接添加到外部布局。你最终可能得到这样的代码:
XML中的
<LinearLayout android:id="@+id/making_dynamically" ... >
然后
LinearLayout outerLayout = (LinearLayout)findViewById(R.id.making_dynamically);
db.open();
Cursor c = db.getAllLocal_Job_Data();
if(c!=null){
if(c.moveToFirst()){
do {
ImageButton imgBtn = new ImageButton(CalendarActivity.this);
imgBtn.setBackgroundResource(R.drawable.color_001);
LinearLayout.LayoutParams imageViewParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
outerLayout.addView(imgBtn, imageViewParams);
} while (c.moveToNext());
}
}
db.close();
答案 1 :(得分:0)
是的,您可以根据数据库创建动态按钮,但为了管理您需要具有可管理的布局,您还可以根据您的按钮创建动态布局,您可以使用表布局,在这种情况下会更好
这是创建动态表布局的那个 -
TableRow tableRow = null;
TextView textView = null;
ImageView imageView = null;
TableLayout tableLayout = (TableLayout)findViewById(R.id.tableLayout);
RelativeLayout relativeLayout = null;
for (String string: listOfStrings)
{
tableRow = new TableRow(this);
relativeLayout = (RelativeLayout) View.inflate(this, R.layout.row, null);
textView = (TextView) relativeLayout.getChildAt(0);
textView.setText(string);
imageView= (ImageView) relativeLayout.getChildAt(1);
imageView.setBackgroundColor(R.color.blue);
//Here's where I would add the parameters...
TableLayout.LayoutParams rlParams = new TableLayout.LayoutParams(FILL_PARENT,
WRAP_CONTENT);
tableRow.addView(relativeLayout, rlParams);
tableLayout.addView(tableRow);
}
或者
TableLayout top = (TableLayout) findViewById(R.id.top_table_layout);
TableLayout bottom = (TableLayout) findViewById(R.id.bottom_table_layout);
TableLayout main = (TableLayout) findViewById(R.id.main);
TableRow top_row = new TableRow(Guesspic.this);
TableRow bottom_row = new TableRow(Guesspic.this);
TableRow main_row = new TableRow(Guesspic.this);
bottom_row.setLayoutParams(new android.widget.TableRow.LayoutParams(
android.widget.TableRow.LayoutParams.MATCH_PARENT,
android.widget.TableRow.LayoutParams.MATCH_PARENT));
top_row.setLayoutParams(new android.widget.TableRow.LayoutParams(
android.widget.TableRow.LayoutParams.MATCH_PARENT,
android.widget.TableRow.LayoutParams.MATCH_PARENT));
main_row.setLayoutParams(new android.widget.TableRow.LayoutParams(
android.widget.TableRow.LayoutParams.MATCH_PARENT,
android.widget.TableRow.LayoutParams.MATCH_PARENT));
Button below_button1 = new Button(Guesspic.this);
below_button1.setText("A");
below_button1.setTag(8);
below_button1.setLayoutParams(new android.widget.TableRow.LayoutParams(
60, 60));
top.addView(below_button1);
听说你可以采用布局参数来维护布局
如果这不起作用,请尝试将边距添加到每个按钮的位置
innerRelativeLayout.addView(imgBtn, new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
之后加上---
imageViewParams.setMargins(30, 20, 30, 0);
希望这可以帮助你..