当前位置: 移动技术网 > IT编程>移动开发>Android > Android编程实现自定义Dialog的大小自动控制方法示例

Android编程实现自定义Dialog的大小自动控制方法示例

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

周杰忠,4人放生落水失踪,山西经济资讯频道

本文实例讲述了android编程实现自定义dialog的大小自动控制方法。分享给大家供大家参考,具体如下:

android应用开发中,无论是出于功能还是增加用户体验,弹出对话框(dialog)进行一些操作提示是非常必要的。android系统有自带的各种样式的对话框,但是根据项目需要可能从效果上满足不了需求,只时我们就要自定义对话框。

我们可以自定义dialog的样式及展示布局,做出我们想要的对话框,但有的时候,我们做出的对话框要么显示太大,要么显得太小,或者是在不同的页面大小不一样,需要做个统一!此时我们就需要对dialog大小进行控制,今天就简单地讲下这个。贴出代码,注释中有详细说明。

先是我们自定义dialog的布局:

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="141dp"
  android:orientation="vertical" >
  <linearlayout
    android:layout_width="match_parent"
    android:layout_height="92dp"
    android:background="@drawable/dialogbackground"
    android:gravity="center"
    android:orientation="vertical" >
    <textview
      android:id="@+id/dialog_tips_tv"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:layout_margintop="10dp"
      android:linespacingextra="3dp"
      android:linespacingmultiplier="1.2"
      android:textcolor="#333333"
      android:textsize="15sp"
      android:visibility="gone" />
    <textview
      android:id="@+id/dialog_content_tv"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="center"
      android:linespacingextra="3dp"
      android:linespacingmultiplier="1.2"
      android:layout_marginleft="15dp"
      android:layout_marginright="15dp"
      android:text="@string/my_bank_delete_dialog"<!--这里是提示文字,可以在代码中更改-->
      android:layout_margintop="3dp"
      android:textcolor="#333333"
      android:textsize="15sp" />
  </linearlayout>
  <linearlayout
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:gravity="center"
    android:orientation="horizontal" >
    <button
      android:id="@+id/fail"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/dialogsuccess_bg"
      android:gravity="center"
      android:text="取消"
      android:textcolor="#007aff"
      android:textsize="17sp" />
    <button
      android:id="@+id/success"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_weight="1"
      android:background="@drawable/dialogerror"
      android:gravity="center"
      android:text="确定"
      android:textcolor="#007aff"
      android:textsize="17sp" />
  </linearlayout>
</linearlayout>

下面就是对话框的实现代码:

首先在所在的类中定义

private dialog mdialog;
//下面是弹出对话框的方法,在需要弹出对话框的地方调用就可以了,当然可以去掉方法,直接写对话框代码也行。
protected void showisdeletedialog() {
view view = layoutinflater.from(getactivity()).inflate(r.layout.common_no_title_dialog, null);
textview tv = (textview) view.findviewbyid(r.id.dialog_content_tv);
tv.settext("您要进行如下操作吗?");//这就是上面说到的提示文字,可以在这里做修改
button mcancel = (button) view.findviewbyid(r.id.success);
button msure= (button) view.findviewbyid(r.id.fail);
// 取消操作
mcancel.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
mdialog.dismiss();
}
});
//确定操作
msure.setonclicklistener(new onclicklistener() {
@override
public void onclick(view v) {
clearrecordrequest();
mdialog.dismiss();
}
});
mdialog = new dialog(getactivity(), r.style.isdeldialog);//自定义的样式,没有贴出代码来
mdialog.setcontentview(view);
mdialog.show();
window dialogwindow = mdialog.getwindow();
windowmanager m = getactivity().getwindowmanager();
display d = m.getdefaultdisplay(); // 获取屏幕宽、高度
windowmanager.layoutparams p = dialogwindow.getattributes(); // 获取对话框当前的参数值
p.height = (int) (d.getheight() * 0.8); // 高度设置为屏幕的0.6,根据实际情况调整
p.width = (int) (d.getwidth() * 0.8); // 宽度设置为屏幕的0.65,根据实际情况调整
dialogwindow.setattributes(p);
}

代码结束!

更多关于android相关内容感兴趣的读者可查看本站专题:《android开发入门与进阶教程》、《android调试技巧与常见问题解决方法汇总》、《android基本组件用法总结》、《android视图view技巧总结》、《android布局layout技巧总结》及《android控件用法总结

希望本文所述对大家android程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网