做360网站优化快速排,青岛市最大的网络公司是哪里,天津建设招标网站首页,美容医疗 网站建设Android在任意Activity界面弹出一个自定义的对话框#xff0c;效果如下图所示: 准备一张小图片#xff0c;右上角的小X图标64*64#xff0c;close_icon.png#xff0c;随便找个小图片代替#xff1b;
第一步#xff1a;样式添加#xff0c;注意#xff1a;默认在value…Android在任意Activity界面弹出一个自定义的对话框效果如下图所示: 准备一张小图片右上角的小X图标64*64close_icon.png随便找个小图片代替
第一步样式添加注意默认在values-thems下如果版本较高请至values-style.xml内定义将以下代码添加在/resource之前 style nameCustomDialog parentandroid:style/Theme.Dialog!--背景颜色及和透明程度--item nameandroid:windowBackgroundandroid:color/transparent/item!--是否去除标题 --item nameandroid:windowNoTitletrue/item!--是否去除边框--item nameandroid:windowFramenull/item!--是否浮现在activity之上--item nameandroid:windowIsFloatingtrue/item!--是否模糊--item nameandroid:backgroundDimEnabledtrue/item/style!--自定义dialog背景弹框设置--style namemydialog parentandroid:style/Theme.Dialog!-- 背景透明,设置圆角对话框必须设置背景透明否则四角会有背景色小块--item nameandroid:windowBackgroundandroid:color/transparent/item!-- 没有标题 --item nameandroid:windowNoTitletrue/item!-- 背景模糊 --item nameandroid:backgroundDimEnabledtrue/item/style
第二步专门为它创建两个类DialogView DialogManager
//DialogView.java
package com.example....//my packageimport android.app.Dialog;
import android.content.Context;
import android.view.Window;
import androidx.annotation.NonNull;public class DialogView extends Dialog {public DialogView(NonNull Context context, int layout, int style, int gravity) {super(context, style);setContentView(layout);Window mWindow getWindow();}
}
//DialogManager.java
package com.example....//my packageimport android.content.Context;
import android.view.Gravity;public class DialogManager {private static volatile DialogManager mInstance null;private DialogManager() { }public static DialogManager getInstance() {if (mInstance null) {synchronized (DialogManager.class) {if (mInstance null) {mInstance new DialogManager();}}}return mInstance;}public DialogView initView(Context context, int layout) {return new DialogView(context,layout, R.style.CustomDialog, Gravity.CENTER);}public DialogView initView(Context context,int layout,int gravity) {return new DialogView(context,layout, R.style.mydialog, gravity);} public void show(DialogView view) {//Showif (view ! null) {if (!view.isShowing()) {view.show();}}}public void hide(DialogView view) {//Hideif (view ! null) {if (view.isShowing()) {view.dismiss();}}}
}第三步给它创建样式布局xml my_dlg_layout.xml
?xml version1.0 encodingutf-8?
LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/androidandroid:layout_widthmatch_parentandroid:backgroundcolor/whiteandroid:layout_heightmatch_parentLinearLayoutandroid:layout_widthmatch_parentandroid:orientationverticalandroid:layout_marginTop5dpandroid:layout_heightwrap_contentRelativeLayoutandroid:layout_widthmatch_parentandroid:orientationhorizontalandroid:layout_height34dpRelativeLayoutandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:gravitycenterandroid:orientationhorizontalTextViewandroid:layout_widthwrap_contentandroid:layout_height25dpandroid:textMyDialogandroid:textColorcolor/redandroid:textSize16spandroid:textStylebold //RelativeLayoutRelativeLayoutandroid:layout_alignParentRighttrueandroid:layout_widthwrap_contentandroid:orientationhorizontalandroid:layout_gravityrightandroid:layout_marginRight10dpandroid:layout_heightwrap_contentImageViewandroid:idid/btn_cancelandroid:layout_width25dpandroid:srcdrawable/close_iconandroid:layout_margin5dpandroid:layout_height25dp//RelativeLayout/RelativeLayoutViewandroid:layout_widthmatch_parentandroid:backgroundcolor/grayandroid:layout_height1dp/LinearLayoutandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:orientationverticalEditTextandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_margin10dpandroid:hint名称华山一区...android:textSize12sp/EditTextEditTextandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:layout_margin10dpandroid:textSize12spandroid:hint备注.../EditTextButtonandroid:layout_widthmatch_parentandroid:layout_heightwrap_contentandroid:idid/MY_Test_Addandroid:backgroundcolor/redandroid:textColorcolor/whiteandroid:layout_margin10dpandroid:paddingTop10dpandroid:paddingBottom10dpandroid:text添加/Button/LinearLayout/LinearLayout/LinearLayout
//这里用到了刚才提到的close_icon随便替换为你的一个小图标
第四步优化-圆角可有可无
?xml version1.0 encodingutf-8?
shape xmlns:androidhttp://schemas.android.com/apk/res/androidcorners android:radius10dp/solid android:color#FFEEEE /
/shape
//注意文件路径res/drawable/shapes.xml添加进去别和你的东西冲突了注意着点边框颜色随便调整
第五步已经完成了分两步显示它初始化显示
import android.view.Gravity;//needed//myActivity(){.....private DialogView mDlgView;//公共变量
private ImageView btnCancel;//公共变量//protected void onCreate(Bundle savedInstanceState) {//my onCreate
//super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);mDlgView DialogManager.getInstance().initView(this, R.layout.my_dlg_layout, Gravity.BOTTOM);//这里要注意这个对话框的View要单独绑定自己的布局
mDlgView.setCanceledOnTouchOutside(false);//这是设置区域外点击是否取消显示
btnCancel mDlgView.findViewById(R.id.btn_cancel);//注意这个关闭图片X在对话框布局里了而不是在当前页面布局不可用this.findViewBy...btnCancel.setOnClickListener(new OnClickListener() {//给返回按纽添加点击隐藏事件Overridepublic void onClick(View view) {DialogManager.getInstance().hide(mDlgView);}
});
初始化完毕在需要的地方进行调用比如你的按钮被点击了直接在里调用这一句即可
DialogManager.getInstance().show(mDlgView);
更多操作提示
//mDlgView.dismiss(); //取消
//mDlgView.setCanceledOnTouchOutside(true);//允许区域外点击关闭
//mDlgView.setCanceledOnTouchOutside(false);//禁止区域外点击关闭//每次显示的时候其实应该清空Edittext里面的内容返回关闭X的图标的ID都能绑定了相同的方法上面的任何子控件绑定都是小菜一碟给个ID用mDialogView.findViewById(R.....)就出来了//my_dlg_layout.xml 样式随便调 padding是内部边距margin是外边距
//那一根线条的颜色也是可调的高度为1的View,android:backgroundcolor/gray,你甚至可以改为android:background#AAAAAA举一反三祝你成功