2011-08-28
my first android app---- 搖搖樂 第一版即將問世
這周總算做了點事情,為結果頁增加了icon和pic
總結一下
問題: 為AlertDialog 增加圖片,圖片個數按照結果對象個數對應顯示
查閱 AlertDialog sdk 發現比較好的方法是建立FrameLayout 然後在裡邊增加imageview,最後 set 到 dialog屬性裡
但 FrameLayout 怎麼建立,費了一些周折才弄出來,不能在同一個activity的layout裡增加framelayout
會出現the specified child all ready has a parent 這樣的錯誤
需要建立個 propalterdialog.xml 單獨定義並安放layout,再調用 LayoutInflater 將這個布局換到當前layout裡顯示
FrameLayout的xml裡可以什麼不定義ImageView標籤
用framelayout的上下文 動態產生image
但出現個問題 framelayout裡的圖片會重疊,使用 LinearLayout則正常 xml裡設定 android:orientation="horizontal" 按行去布局,將圖片放在一行
以下是用到的所有代碼
LayoutInflater factory = LayoutInflater.from(DemoActivity.this);
LinearLayout fl = (LinearLayout) factory.inflate(R.layout.propalterdialog,null);
int size = selectionList.size();
for(Selection sel:selectionList){
ImageView imageView = new ImageView(fl.getContext());
imageView.setLayoutParams(new FrameLayout.LayoutParams(190/size, 190/size, 100));
int image_id = DemoActivity.this.getResources().getIdentifier(sel.getPicture(), "drawable",
"com.netqin.android.lab");
imageView.setImageResource(image_id);
fl.addView(imageView);
}
new AlertDialog.Builder(DemoActivity.this).setTitle("result")
.setIcon(R.drawable.result_icon)
.setView(fl)
.setMessage(result).setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
}