0

正如我在标题中所描述的,我在 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以前创建的按钮,但我找不到如何将它放在这里。我尝试了一些想法,但并没有改变任何事情。所以请任何人有任何想法来解决我的问题,然后请与我分享。

4

2 回答 2

0

Yes you are able to create dynamic buttons according to database but to manage that you need to have manageable layout you can also create dynamic layout according to your buttons you can use Table layout which would be much better in this case

This is the one to crate dynamic table layout-

     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);
    } 

Or

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);

hear you can have taken layout params to maintain the layouts

if this will not work then try to add Margins to the position of every single button

innerRelativeLayout.addView(imgBtn, new RelativeLayout.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT,       
   ViewGroup.LayoutParams.WRAP_CONTENT));

after this add---

   imageViewParams.setMargins(30, 20, 30, 0);  

Hope this could help you..

于 2013-06-25T10:34:30.350 回答
0

您正在正确地创建多个 ImageButton,但是您将它们添加到同一个位置的相对布局中(特别是,将内部相对布局添加到外部相对布局)。我不知道您要实现什么布局,但您可以尝试将外部布局更改为 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();
于 2013-06-25T10:30:49.103 回答