使Android应用的AlertDialog对话框中的按钮显示为ImageButton图标,并设置相应

来源:互联网 发布:镜面蛋糕走红网络 编辑:程序博客网 时间:2024/06/02 05:07

最终示意图如下所示:



首先需要新建一个layout文件:new_layout.xml: 这里定义有三个按钮

<?xml version="1.0" encoding="UTF-8" ?><LinearLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_height="wrap_content"    android:layout_width="wrap_content"    android:layout_margin="15dp"    android:gravity="center_horizontal"    android:background = "#FFFFFF"    android:orientation="horizontal">        <!-- game button -->    <ImageButton         android:id="@+id/game"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:layout_margin="5dp"        android:layout_gravity="bottom"        android:background = "#00ffffff"        android:src="@drawable/game"/>        <!-- browser button -->    <ImageButton         android:id="@+id/browser"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:layout_margin="5dp"        android:layout_gravity="bottom"        android:background = "#00ffffff"        android:src="@drawable/browser"/>   <!-- email button -->    <ImageButton         android:id="@+id/email"        android:layout_height="wrap_content"        android:layout_width="wrap_content"        android:layout_margin="5dp"        android:layout_gravity="bottom"        android:background = "#00ffffff"        android:src="@drawable/email"/>    </LinearLayout>


其次在你需要调用弹出框的地方添加如下代码:

final AlertDialog alertDialog = new AlertDialog.Builder(TalkerActivity.this).create();        alertDialog.show();        Window win = alertDialog.getWindow();        //设置自定义的对话框布局        win.setContentView(R.layout.new_layout);                //游戏按钮事件        ImageButton game_btn = (ImageButton)win.findViewById(R.id.game);        game_btn.setOnClickListener(new OnClickListener(){            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                            }        });                //浏览器程序        ImageButton browser_btn = (ImageButton)win.findViewById(R.id.browser);        browser_btn.setOnClickListener(new OnClickListener(){            @Override            public void onClick(View v) {                // TODO Auto-generated method stub            }        });                //电子邮件程序        ImageButton email_btn = (ImageButton)win.findViewById(R.id.email);        email_btn.setOnClickListener(new OnClickListener(){            @Override            public void onClick(View v) {                // TODO Auto-generated method stub                        }        });    


参考链接:http://5e76.net/show-4373.html


原创粉丝点击