开发时我们可能需要一些非全屏view的一些提示信息,或者不想去创建需要在清单文件注册的Activity来显示view,那么我们就需要借助一些其他的窗体子类来完成需求如:Dialog , PopuWindow,本文介绍Dialog
很简单,无需设置任何style什么的
public class MyDialog extends Dialog {
private View contentView;
private TextView child;
private RelativeLayout pb;
public MyDialog(@NonNull Context context) {
super(context);
}
/**
* 强烈建议在此方法进行自定义view的初始化操作
* @param savedInstanceState
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//去除Dialog本身的背景
initCustomTheme();
createContentView();
setContentView(contentView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
}
private void createContentView() {
contentView = View.inflate(getContext(), R.layout.mydialog, null);
child = contentView.findViewById(R.id.tv);
pb = contentView.findViewById(R.id.pb);
child.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pb.setVisibility(View.VISIBLE);
}
});
}
private void initCustomTheme() {
Window window = getWindow();
if (window != null){
window.setGravity(Gravity.TOP);//显示在窗口哪个位置,默认中间
window.setDimAmount(0f);//把背景后面的模糊去掉
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));//去掉dialog本身的矩形区域去掉(当我们设置自己的圆角框有用)
//window.setWindowAnimations(R.style.DialogAnimation);//动画
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/mydialog"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Are you sure exit ?" />
<View
android:layout_below="@id/tv"
android:layout_marginTop="15dp"
android:background="#bbffaa"
android:layout_width="match_parent"
android:layout_height="1dp"/>
<LinearLayout
android:layout_below="@id/tv"
android:layout_marginTop="15dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="back"
android:textColor="#eeff00"
android:gravity="center"
android:padding="10dp"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<View
android:background="#bbffaa"
android:layout_width="1dp"
android:layout_height="match_parent"/>
<TextView
android:text="sure"
android:textColor="#eeff00"
android:gravity="center"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="@+id/pb"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:visibility="gone">
<ProgressBar
android:indeterminateTint="#fff000"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
</RelativeLayout>
</FrameLayout>
标签:contentView,自定义,void,private,window,Dialog,Android,View
From: https://www.cnblogs.com/maowuge/p/16602024.html