android中自己定义dialog
2014-07-18 12:34
337 查看
dialog是android中提供的一组弹出提示框,非常好用,可是它的样式是一个定式,有时候我们需求定义一些自己的样式.
1,定义一个样式文件,此文件继承自Theme.Dialog,在style.xml文件中建立一个自己的样式
2, 新建一个layout,做为弹出框的显示
3,调用此dialog
按钮点击事件:
有时候,我们会想设置dialog的宽或高,这个还是比较简单的,直接在dialog.show()下面添加如下代码:
1,定义一个样式文件,此文件继承自Theme.Dialog,在style.xml文件中建立一个自己的样式
<style name="addNoteType_error_Dialog" parent="@android:Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@color/color_shenhui</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> </style>
2, 新建一个layout,做为弹出框的显示
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:background="#DFDFDF"> <TextView android:layout_width="fill_parent" android:layout_height="50dp" android:gravity="center_vertical" android:layout_marginLeft="5dp" android:text="@string/txt_addnoteType_error_title" android:textColor="#00CCFF" android:textSize="18sp" /> <View android:layout_width="fill_parent" android:layout_height="1px" android:background="#00CCFF" /> <TextView android:layout_width="fill_parent" android:layout_height="50dp" android:gravity="left|center" android:layout_marginTop="5dp" android:text="@string/txt_addnoteType_error_content_null" /> <View android:layout_width="fill_parent" android:layout_height="1px" android:background="#BBB9BA" android:layout_marginTop="5dp" /> <Button android:id="@+id/btn_add_note_addnotetype_Error_ok" android:layout_width="fill_parent" android:layout_height="50dp" android:text="@string/txt_ok" android:background="@null" /> </LinearLayout>
3,调用此dialog
//此处直接new一个Dialog对象出来,在实例化的时候传入主题 dialog = new Dialog(Sel_NoteTypeActivity.this, R.style.addNoteType_error_Dialog); //设置它的ContentView dialog.setContentView(R.layout.dialog_addnotetype_error); Button btn_add_note_addnotetype_Error_ok = (Button)dialog.findViewById(R.id.btn_add_note_addnotetype_Error_ok); btn_add_note_addnotetype_Error_ok.setOnClickListener(new addnoteTypeErroClickListener()); dialog.show();
按钮点击事件:
class addnoteTypeErroClickListener implements OnClickListener{ @Override public void onClick(View v) { // TODO Auto-generated method stub dialog.cancel(); } }
有时候,我们会想设置dialog的宽或高,这个还是比较简单的,直接在dialog.show()下面添加如下代码:
WindowManager.LayoutParams Layoutparams = dialog.getWindow().getAttributes(); Layoutparams.width = (int)(mScreenWidth *9 / 10); //设置宽度 dialog.getWindow().setAttributes(Layoutparams);
相关文章推荐
- android ui定义自己的dialog(项目框架搭建时就写好,之后事半功倍)
- 自己定义Android Dialog
- android--自己定义ProgressDialog显示位置(其他Dialog子类都能够设置)
- android ui定义自己的dialog
- android 自己定义dialog并实现失去焦点(背景透明)的功能
- Android 对话框(Dialog) 及 自己定义Dialog
- Android自己定义dialog中的EditText无法弹出键盘的解决
- android怎样写一个自己定义的dialog能够在Title的位置弹出来
- android ui定义自己的dialog(项目框架搭建时就写好,之后事半功倍)
- android 实现自己定义状态栏通知(Status Notification)
- Android开发--身高体重指数(BIM)计算--添加对话框(Dialog)(设计对话框、定义调用点、实体对话框、重构、添加按钮)
- Android自己定义控件而且使其能够在xml中自己定义属性
- Android中通过反射获取资源Id(特别用在自己定义一个工具将其打成.jar包时,特别注意资源的获取)
- Android中自己定义一个shade.xml
- Android中常用适配器及定义自己的适配器
- Android开发之自己定义TabHost文字及背景(源码分享)
- Android中常用适配器及定义自己的适配器
- Android中常用适配器及定义自己的适配器
- android dialog 原来dialog对话框也有自己的按键监听事件 onKeyDown方法
- android 自定义Dialog背景透明及显示位置设置