当前位置: 移动技术网 > 移动技术>移动开发>Android > Android编程中自定义dialog用法实例

Android编程中自定义dialog用法实例

2019年07月24日  | 移动技术网移动技术  | 我要评论

本文实例讲述了android编程中自定义dialog用法。分享给大家供大家参考,具体如下:

dialog是android中提供的一组弹出提示框,非常好用,可是它的样式是一个定式,有时候我们需求定义一些自己的样式

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程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网