android中自定义dialog的使用方法简介

来源:互联网 发布:下载炫浪网络社区app 编辑:程序博客网 时间:2024/06/04 18:34

在android的界面开发中,经常要用到一些自定义对话框的情况,比如采用dialog的方式实现弹出式菜单等等,现在把自定义对话框的使用方法进行简介,以备后用。

第一步,自定义layout文件。新建一个layout的xml文件,对你的自定义对话框进行页面显示设计。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >        <TextView           android:id="@+id/deleteitem"           android:layout_width="match_parent"           android:layout_height="48sp"           android:gravity="center"           android:singleLine="true"           android:text="删  除"           android:textSize="18sp" />                            <ImageView            android:layout_width="match_parent"            android:layout_height="0.5dp"            android:background="#000000" />                 <TextView           android:id="@+id/dialnumber"           android:layout_width="match_parent"           android:layout_height="48sp"           android:gravity="center"           android:singleLine="true"           android:text="拨  号"           android:textSize="18sp" />         <ImageView            android:layout_width="match_parent"            android:layout_height="0.5dp"            android:background="#000000" />  </LinearLayout>

第二步,创建一个类,继承自Dialog。

第三步,将layout文件与类进行关联。
这里有几点需要注意,
1)在oncreat方法中运用setContentView(R.layout.turnupmenu);方法进行总体的关联,然后用findViewById对一些具体的空间进行关联。
2)在构造函数中要传一个theme进去,通过这个theme对对话框的主题进行设计。

public TurnUpMenu(Context context,int theme) {        super(context,theme);    }

因为默认的theme很难看,有title,而且显示的很难看,所以一定要自定义一个theme,才能使自己的对话框好看一些,这一点很关键,开始的时候我做了一个自定义对话框,总是和layout中设计的不一致,后来经过查找资料才发现是这个theme有问题,最后在style中重新定义了一个theme之后,就对了。

<style name="dialog" parent="@android:style/Theme.Holo.Light.Dialog">        <!-- 设置对话框没有标题 -->        <item name="android:windowNoTitle">true</item>         <!-- 设置对话框不分层 -->                <item name="android:windowContentOverlay">@null</item>        <!-- 设置对话框在横屏状态下所占屏幕宽度的比例 -->         <item name="android:windowMinWidthMajor">95%</item>         <!-- 设置对话框在竖屏状态下所占屏幕宽度的比例 -->          <item name="android:windowMinWidthMinor">75%</item>      </style>
0 0
原创粉丝点击