2013-06-25 29 views
0

正如我在title中所述,我在sqlite中有一個本地數據庫。我想以參數方式創建ImageButton。本地數據庫循環執行多少次?請參閱下面的代碼:以編程方式創建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

您正在創建多個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(); 
+0

它的非常好的方式來創建動態UI ......你救我太多的時間........謝謝主席先生.....謝謝非常多........ – user2507920

+0

先生,我有一個問題:現在我應該怎麼做,如果我想在imageButton的中心添加textview,我的意思是我已經添加textview動態y在我的代碼,但由此屬性應該使用如此的文本視圖進入ImageButton的中心? – user2507920

+0

@ user2507920我不太清楚爲什麼你想這麼做,說實話。但是,如果不是每個ImageButton都需要圖像按鈕和另一個控件的組合,我建議您使用該組合創建一個XML,然後在一個循環中充氣該XML並添加到佈局中。 –

0

是的,你可以根據數據庫中創建動態按鈕,但管理,你需要有可管理的佈局,你也可以根據你的按鈕創建動態佈局,你可以使用表格佈局,在這種情況下會好得多

這是一個箱d ynamic表於版圖

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

聽說你可以採取佈局PARAMS維持佈局

如果這不會工作,那麼嘗試添加邊距的每一個位置按鈕

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

此後添加---

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

希望這可以幫助你..

相關問題