android中的对话框:自定义对话框
2014-12-24 13:17
411 查看
首先看下效果图下面讲一下具体的实现:1.修改系统默认的Dialog样式(风格、主题)2.自定义Dialog布局文件3.可以自己封装一个类,继承自Dialog或者直接使用Dialog类来实现,为了方便以后重复使用,建议自己封装一个Dialog类=====================================================第一步: 我们知道Android定义个控件或View的样式都是通过定义其style来实现的,查看Android框架中的主题文件,在源码中的路径:/frameworks/base/core/res/res/values/themes.xml,我们可以看到,Android为Dialog定义了一个样式,我们可以看到,在Themes.xml中定义的Dialog的样式,其中,定义了window的标题样式,window的背景图,是否悬浮等等。 那么,我们要创建具有自定义样式的Dialog就可以创建一个styles.xml,在其中定义我们自己的Dialog样式,让其继承自Theme.Dialog样式,并修改其中的某些属性即可。 定义我们自己的Dialog样式: a.创建一个styles.xml文件,放在res/values文件夹下(当然了,这就不用说了。。。啰嗦一下) b.在styles.xml中定义Dialog样式,代码如下: 这里使用了一个垂直方向的线性布局,并且设置所有子元素居中,子元素为已个进度条ProgressBar和一个TextView。 此处,ProgressBar采用自定义样式,使用系统默认的ProgressBar可达到同样的效果(大同小异)。LinearLayout的背景
<!--Defaultthemefordialogwindowsandactivities,whichisusedbythe {@linkandroid.app.Dialog}class.Thischangesthewindowtobe floating(notfilltheentirescreen),andputsaframearoundits contents.Youcansetthisthemeonanactivityifyouwouldliketo makeanactivitythatlookslikeaDialog.--> <stylename="Theme.Dialog"> <itemname="android:windowFrame">@null</item> <itemname="android:windowTitleStyle">@android:style/DialogWindowTitle</item> <itemname="android:windowBackground">@android:drawable/panel_background</item> <itemname="android:windowIsFloating">true</item> <itemname="android:windowContentOverlay">@null</item> <itemname="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <itemname="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<stylename="Theme_dialog"parent="@android:style/Theme.Dialog"> <itemname="android:windowBackground">@android:color/transparent</item> <itemname="android:windowNoTitle">true</item> </style>上面代码中,定义了一个样式Theme_dialog,继承自@android:style/Theme.Dialog,然后定义了Dialog所在Window的背景图,此处使用的是透明颜色#00000000,然后定义了Dialog所在的Window隐藏标题(系统定义Dialog样式是带有标题的,在此设置此属性为true可隐藏标题),自定义Dialog样式到这就完成了。第二步: 定义Dialog的布局: 创建一个布局文件layout_dialog.xml,代码如下:
1<?xmlversion="1.0"encoding="utf-8"?> 2<LinearLayout 3xmlns:android="http://schemas.android.com/apk/res/android" 4android:orientation="vertical" 5android:layout_width="fill_parent" 6android:layout_height="fill_parent" 7android:background="@drawable/pp_bg_dialog" 8android:gravity="center"> 9 10<ProgressBarandroid:layout_width="wrap_content" 11android:layout_height="wrap_content" 12style="@style/progressbar_normal"/> 13 14<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content" 15style="@style/text_white_small"android:layout_marginTop="5dp" 16android:text="正在删除..."android:id="@+id/message"/> 17</LinearLayout>
android:background="@drawable/pp_bg_dialog"(即上面效果图中居中显示的黑色透明背景框)是一个自定义的图片资源Shape:
1<?xmlversion="1.0"encoding="utf-8"?> 2<shape 3xmlns:android="http://schemas.android.com/apk/res/android" 4android:shape="rectangle"> 5<cornersandroid:radius="10dp"/> 6<solidandroid:color="#55000000"/> 7 8</shape>
代码中定义了一个矩形,并设置了圆角和颜色。到此,Dialog的布局就完成了。
第三步:
自定义CustomDialog类,继承自Dialog,代码如下:
1publicclassCustomDialogextendsDialog{23privatestaticintdefault_width=160;//默认宽度4privatestaticintdefault_height=120;//默认高度56publicCustomDialog(Contextcontext,intlayout,intstyle){7this(context,default_width,default_height,layout,style);8}910publicCustomDialog(Contextcontext,intwidth,intheight,intlayout,intstyle){11super(context,style);1213//setcontent14setContentView(layout);1516//setwindowparams17Windowwindow=getWindow();18WindowManager.LayoutParamsparams=window.getAttributes();1920//setwidth,heightbydensityandgravity21floatdensity=getDensity(context);22params.width=(int)(width*density);23params.height=(int)(height*density);24params.gravity=Gravity.CENTER;2526window.setAttributes(params);27}2829privatefloatgetDensity(Contextcontext){30Resourcesresources=context.getResources();31DisplayMetricsdm=resources.getDisplayMetrics();32returndm.density;33}343536}
在构造方法中设置了Dialog的contentView,并且设置了Window的宽度、高度和居中显示。
CustomDialog使用方法如下:
1publicclassCustomDialogDemoActivityextendsActivity{23@Override4protectedvoidonCreate(BundlesavedInstanceState){5super.onCreate(savedInstanceState);6setContentView(R.layout.test);78CustomDialogdialog1=newCustomDialog(this,R.layout.common_dialog,R.style.Theme_dialog);//Dialog使用默认大小(160)9CustomDialogdialog2=newCustomDialog(this,180,180,R.layout.common_dialog,R.style.Theme_dialog);10dialog2.show();//显示Dialog11 //如果要修改Dialog中的某个View,比如把"正在删除..."改为"加载中..."12TextViewmMessage=(TextView)dialog2.findViewById(R.id.message);13mMessage.setText("加载中...");14}15}
OK,就到此结束吧,欢迎各位自主扩展,创建具有自主特色的Dialog~~~
相关文章推荐
- Android---功能模块一之颜色选择器自定义Preference 对话框
- Android自定义对话框列表
- Android自定义对话框的大小
- Android对话框的自定义及透明效果
- android自定义大小对话框
- android 自定义对话框 背景透明
- Android自定义Dialog对话框
- Android自定义对话框
- android 自定义对话框 背景透明
- android自定义对话框
- 20. android dialog——自定义对话框之一
- Android简明开发教程十八:自定义对话框 Transform
- Android简明开发教程十八:自定义对话框 Transform
- Android 对话框进阶(自定义)
- android自定义对话框
- Android Dialog( 自定义对话框)
- Android简明开发教程十八:自定义对话框 Transform
- android 自定义对话框
- android Dialog 各种Dialog 三个选项的、进度条的、单选的、多选的、自定义的对话框
- android Dialog 各种Dialog 三个选项的、进度条的、单选的、多选的、自定义的对话框