【android】add image in alertdialog

來源:互聯網
上載者:User

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

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.