正如我在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
與以前創建的按鈕,但我無法找到如何把它放在這裏。我嘗試了一些想法,但沒有改變任何事情。所以請任何人有任何想法解決我的問題,然後請與我分享。
它的非常好的方式來創建動態UI ......你救我太多的時間........謝謝主席先生.....謝謝非常多........ – user2507920
先生,我有一個問題:現在我應該怎麼做,如果我想在imageButton的中心添加textview,我的意思是我已經添加textview動態y在我的代碼,但由此屬性應該使用如此的文本視圖進入ImageButton的中心? – user2507920
@ user2507920我不太清楚爲什麼你想這麼做,說實話。但是,如果不是每個ImageButton都需要圖像按鈕和另一個控件的組合,我建議您使用該組合創建一個XML,然後在一個循環中充氣該XML並添加到佈局中。 –