Android中自定义dialog方法,可直接调用,更改样式
2015-08-07 13:22
656 查看
先定义好dialog_delete.xml文件:
java代码:
运行效果截图:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:background="#ffffff"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_margin="15px"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="40px" android:text="是否删除任务/卸载程序?" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/negativeButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="取消" android:layout_marginRight="50px"/> <Button android:id="@+id/positiveButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="确定" android:layout_marginLeft="50px"/> </LinearLayout> </LinearLayout>
java代码:
//显示对话框 private void showNormalDia() { LinearLayout layout = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.dialog_delete, null ); //对话框 Dialog dialog = new AlertDialog.Builder(MainActivity.this).create(); dialog.show(); dialog.getWindow().setContentView(layout); //取消按钮 Button negativeButton = (Button) layout.findViewById(R.id.negativeButton); negativeButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "cancel", Toast.LENGTH_SHORT).show(); } }); //确定按钮 Button positionButton = (Button) layout.findViewById(R.id.positiveButton); positionButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getApplicationContext(), "ok", Toast.LENGTH_SHORT).show(); } }); }
运行效果截图:
相关文章推荐
- Android Xutils 框架的介绍
- Android开发中设置系统全局属性方法总结
- Android项目打包成APK文件
- 安卓界面跳转时取消系统自带的动画
- Android 5.1 Phone MT(来电)流程分析(Framework层)
- 【干货】如何简单粗暴解决Android Activity泄漏问题
- android学习笔记(3)Button控件的学习
- 打造一个全命令行的Android构建系统
- recyclerview的使用,左滑删除
- 认识 Android 中的 Activity 组件 总结
- Android 存储:Internal Storage的用法以及与External storage 的区别
- 判断agent(是电脑还是手机,是Android还是IOS)
- Android主线程到底是什么(一)
- android动画学习心得
- Android中的Touch事件
- Android自定义View 画弧形,文字,并增加动画效果
- 转:Android--JSONArray排序 - 掌缘生灭
- Android中通过typeface设置字体
- has leaked window com.android.internal.policy.impl.PhoneWindow$ that was originally added here异常解决
- 关于计算两个时间间隔问题