知识记录三:自定义Dialog
来源:互联网 发布:拼接屏软件 编辑:程序博客网 时间:2024/06/05 03:42
Android中作为与用户互动的控件之一Dialog,系统提供的AlertDialog和Dialog尽管可以达到我们的目的,但是那戳的不行的外观大多情况下总是不能让我们满意,再加上设计师追求完美与整体风格的一致性,使得自定义Dialog成为开发软件中不可缺少的一环。
自定义Dialog一般会经过下面几个步骤
1) 要自定义Dialog,自然要先继承自Dialog
2) 按照需求设置styles,dialog应用样式后会呈现不同的效果
<style name="CustomDialog" parent="android:style/Theme.Dialog"> <!-- 是否有标题 --> <item name="android:windowNoTitle">true</item> <!-- 设置dialog的背景为透明 --> <item name="android:background">@android:color/transparent</item> <!-- 背景颜色及透明程度 --> <item name="android:windowBackground">@android:color/transparent</item> <!-- 背景缓存色无 --> <item name="android:colorBackgroundCacheHint">@null</item> <!-- 是否浮现在activity之上 --> <item name="android:windowIsTranslucent">true</item></style>
3) dialog布局,想显示成什么样子就布局成什么样子就好了,我这里就是一个删除选择对话框
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="230.0dip" android:layout_marginLeft="75.0dip" android:layout_marginRight="75.0dip" android:orientation="vertical" android:background="@color/dialog_bg_normal"> <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="0.0dip" android:layout_weight="2" android:paddingLeft="30.0dip" android:gravity="center_vertical" android:text="@string/delete_message" android:textSize="28.0sp" android:textColor="@color/item_normal"/> <View android:layout_width="match_parent" android:layout_height="1.0dip" android:background="@color/item_normal"/> <LinearLayout android:layout_width="match_parent" android:layout_height="0.0dip" android:layout_weight="1" android:orientation="horizontal"> <TextView android:id="@+id/ok" android:layout_width="0.0dip" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="@string/delete_ok" android:textSize="26.0sp" android:textColor="@color/item_normal" android:background="@drawable/dialog_btn_bg"/> <View android:layout_width="1.0dip" android:layout_height="match_parent" android:background="@color/item_normal"/> <TextView android:id="@+id/cancel" android:layout_width="0.0dip" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:text="@string/delete_cancel" android:textSize="26.0sp" android:textColor="@color/item_normal" android:background="@drawable/dialog_btn_bg"/> </LinearLayout></LinearLayout>
4)到了这一步就是自定义的Dialog了,应用上面的style和layout,留出必要的事件接口即可
package com.autolink.voicerecord;import android.app.Dialog;import android.content.Context;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.TextView;public class MyDialog extends Dialog implements OnClickListener { private TextView title, ok, cancel; private DelEvent delEvent; public void setDelEvent(DelEvent delEvent) { this.delEvent = delEvent; } public MyDialog(Context context) { super(context,R.style.CustomDialog); setContentView(R.layout.dialog_common); initView(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } private void initView() { title = (TextView) findViewById(R.id.title); ok = (TextView) findViewById(R.id.ok); cancel = (TextView) findViewById(R.id.cancel); ok.setOnClickListener(this); cancel.setOnClickListener(this); } public void showDialog(int resId) { setDialogTitle(resId); show(); } /****设置Dialog的提示文字***/ private void setDialogTitle(int resId){ title.setText(resId); } /***回调接口***/ public interface DelEvent{ public void onConfrim(); } @Override public void onClick(View view) { int what = view.getId(); if (R.id.ok == what) { if (delEvent != null) { delEvent.onConfrim(); } } else if (R.id.cancel == what) { this.cancel(); } }}
这里说明几个点:
- Dialog直到show()才调用了onCreate(),若是将setContentView写在onCreate(),要注意showDialog这个方法中设置标题时可能会出现空指针的情况
- (1)setContentView(int layoutId):如果采用这个方法则可以在XML布局文件设置最外层布局的大小,这样dialog显示的大小就是在布局文件中设置的大小;(2)setContentView(View view):采用这个方法,不管在布局文件中最外层布局文件的宽高为何则均全屏显示,此时我们可以将布局文件次外层布局看做我们想要呈现的布局即可达到效果;
- 此处这里的例子是只有两个按钮的Dialog,事件也就只有两个,若是有三个或者更多,可以通过更改回调接口实现要求(如下所示,)
/***接口定义**/public interface DelEvent{ public void onConfrim(int type);}/***使用***/if (delEvent != null) { delEvent.onConfrim(1);}
5)定义好了,最后就是使用了
在要使用的地方new一个Dialog对象,在需要显示出来的时候调用即可
dialog = new MyDialog(mContext);dialog.showDialog(R.string.delete_message_all);
下面是事件调用
//在适当的地方设置即可dialog.setDelEvent(delEvent);private DelEvent delEvent = new DelEvent() { @Override public void onConfrim() { //do something what you want to do }};
好了,这次的自定义Dialog的记录就到这了,上面只是简单的记录了一种两按钮的dialog的定义与使用过程,其它更麻烦的对话框都可以在此之上进行增加修改
Good Luck!
0 0
- 知识记录三:自定义Dialog
- 三种自定义Dialog方式
- android学习记录1:android自定义Dialog
- 记录:去除自定义dialog的标题
- 自定义View相关知识记录
- 弹窗之三:自定义Dialog
- 安卓自定义Dialog(三)
- 自定义Dialog---继承Dialog
- 自定义Dialog android Dialog
- 自定义Dialog
- 自定义Dialog
- 自定义dialog
- 自定义 Dialog
- 自定义dialog
- 自定义 dialog
- 自定义Dialog
- 自定义 Dialog
- 自定义Dialog
- 在代码中隐藏标题栏
- tolua 学习笔记 插件的使用
- Intellij IDEA使用Maven管理Scala项目
- 什么样的男人才算成熟男人?
- u3d+Litjson保存数据到安卓端
- 知识记录三:自定义Dialog
- javascript之window对象
- HTML DOM Element 对象
- 微信小程序-设置启动页面
- 关于JavaScript的this
- HTML DOM 元素对象
- Linux-使用 Find 命令找到那些需要清理的文件
- 寒假集训日常!
- 飞机大战