Android 根据本地数据库记录以编程方式创建ImageButton
正如我在标题中所描述的,我在sqlite中有一个本地db。我想以参数化方式创建Android 根据本地数据库记录以编程方式创建ImageButton,android,sqlite,Android,Sqlite,正如我在标题中所描述的,我在sqlite中有一个本地db。我想以参数化方式创建ImageButton。本地数据库循环执行多少次?请参阅下面的代码: RelativeLayout outerRelativeLayout = (RelativeLayout)findViewById(R.id.relativeLayout2_making_dynamically); db.open(); Cursor c = db.getAllLocal_Job_Data(); if(
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\u toRightOf,但我找不到如何将它放在这里。我尝试了一些想法,但没有改变任何事情。所以,请任何人有任何想法来解决我的问题,然后请与我分享 您正确地创建了多个ImageButton,但将它们添加到同一位置的相对布局中(具体地说,将内部相对布局添加到外部相对布局中)。我不知道你想要达到什么样的布局,但是你可以试着把外部布局改成线性布局 此外,您实际上不需要内部布局-您可以将ImageButtons直接添加到外部布局。您可能会得到如下代码: 在XML中
<LinearLayout android:id="@+id/making_dynamically" ... >
是的,您可以根据数据库创建动态按钮,但要管理它,您需要具有可管理的布局,您也可以根据按钮创建动态布局,您可以使用表布局,在这种情况下会更好 这是一个板条箱动态表布局-
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);
}
或
听说您可以使用布局参数来维护布局
如果这不起作用,那么尝试在每个按钮的位置添加边距
innerRelativeLayout.addView(imgBtn, new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
在此之后添加---
希望这能帮助您….这是一种动态创建ui的好方法…您为我节省了很多时间…..谢谢您,先生。。。。。非常感谢…….先生,我有一个问题:如果我想在imageButton的中心添加textview,我的意思是我已经在我的代码中添加了textview,但是我应该使用哪个属性才能使textview位于imageButton的中心?@user2507920老实说,我不太确定你为什么要这样做。但是,如果不是每个ImageButton,而是希望图像按钮和另一个控件的组合,我建议您使用该组合创建一个XML,然后在循环中膨胀该XML并添加到布局中。
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);