Android系統(tǒng)默認(rèn)對(duì)話框添加圖片功能
開發(fā)工具Android Studio
今天公司UI要求軟件對(duì)話框改成加圖片的,以前沒有做過,所以就學(xué)習(xí)了一下,廢話不多說,
看效果:
創(chuàng)建XML文件dialog_lsit_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/my_dialog_shape"> <ImageView android:id="@+id/iv_dialog" android:layout_width="wrap_content" android:layout_height="50dp" android:paddingLeft="20dp" android:gravity="center"/> <TextView android:gravity="center" android:id="@+id/tv_dialog" android:layout_width="wrap_content" android:layout_height="50dp" android:textSize="20sp" android:layout_marginLeft="14dp"/> </LinearLayout> Main_activity.xml布局文件 <TextView style="@style/itemTextLeft" android:layout_width="wrap_content" android:layout_height="@dimen/dp_40" android:drawableLeft="@mipmap/zhifufangshi" android:drawablePadding="@dimen/dp_10" android:text="@string/text_zhifufangshi" /> <TextView android:id="@+id/fangshi" android:clickable="true" style="@style/itemTextLeftNext" android:layout_width="match_parent" android:layout_height="@dimen/dp_40" android:gravity="center_vertical|end" android:drawablePadding="@dimen/dp_10" /> MainActivity.java源文件 //支付對(duì)話框 icon or text private String[] datas = {"微信支付", "支付寶支付", "余額支付"}; private int[] images = {R.drawable.weixinzhifu, R.drawable.zhifubaozhifu, R.drawable.yuezhifu}; private List<Map<String, Object>> listItem; public class MainActivity extends BaseActivity implements View.OnClickListener{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initView(); } @Override protected void initView() { findViewById(R.id.fangshi).setOnClickListener(this); } @Override public void onClick(View view) { if (view != null) { switch (view.getId()) { case R.id.fangshi: showFangshi(); break; } } //選擇支付方式 private void showFangshi() { listItem = new ArrayList<Map<String, Object>>(); for (int i = 0; i < datas.length; i++) { Map<String, Object> map = new HashMap<>(); map.put("data", datas[i]); map.put("image", images[i]); listItem.add(map); } SimpleAdapter simpleAdapter = new SimpleAdapter(this, listItem, R.layout.dialog_list_item, new String[]{"data", "image"}, new int[]{R.id.tv_dialog, R.id.iv_dialog}); //自定義列表對(duì)話框 AlertDialog.Builder builder = new AlertDialog.Builder(this).setAdapter(simpleAdapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //Toast.makeText(MainActivity.this, "選中了:"+datas[which].toString(),Toast.LENGTH_SHORT).show(); //支付方式那行顯示名稱 fangshi.setText(datas[which]); //支付方式賦值 payType = which; //關(guān)閉對(duì)話框 dialog.dismiss(); } }); builder.create().show(); }
注意:支付方式那行顯示名稱——關(guān)閉對(duì)話框 里邊的變量是我寫好的 用的時(shí)候可以注釋 解除Toast注釋。因公司軟件 我只是截取部分 用來說明這個(gè)效果 就不上源碼文件了,感謝!有問題可以私信我!
相關(guān)文章
Android動(dòng)畫工具類的封裝實(shí)戰(zhàn)記錄
這篇文章主要給大家介紹了關(guān)于一次Android動(dòng)畫工具類的封裝實(shí)戰(zhàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)各位Android開發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04Android編程實(shí)現(xiàn)PendingIntent控制多個(gè)鬧鐘的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)PendingIntent控制多個(gè)鬧鐘的方法,涉及PendingIntent屬性設(shè)置與使用的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12詳解Android PopupWindow怎么合理控制彈出位置(showAtLocation)
本篇文章主要介紹了詳解Android PopupWindow怎么合理控制彈出位置(showAtLocation),具有一定的參考價(jià)值,有興趣的可以了解一下2017-10-10Android開發(fā)之拼音轉(zhuǎn)換工具類PinyinUtils示例
這篇文章主要介紹了Android開發(fā)之拼音轉(zhuǎn)換工具類PinyinUtils,涉及Android基于pinyin4j-2.5.0.jar包文件實(shí)現(xiàn)漢字轉(zhuǎn)拼音功能的相關(guān)操作技巧,需要的朋友可以參考下2017-11-11