一.自定义dialog的使用
1.定义一个类继承dialog
2.修改style
3.加载布局
4.指定dialog在屏幕中的位置
/自定义一个类继承dialog
public class AdDialog extends Dialog implements View.OnClickListener {
private Context context;
public AdDialog(Context context) {
// 更改样式,把背景设置为透明的
super(context, R.style.LocatonDialogStyle);
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
//加载dialog的布局
setContentView(R.layout.view_location_dialog_dong);
//拿到布局控件进行处理
ImageView imageView = (ImageView) findViewById(R.id.mm);
imageView.setOnClickListener(this);
//初始化布局的位置
initLayoutParams();
}
// 初始化布局的参数
private void initLayoutParams() {
// 布局的参数
LayoutParams params = getWindow().getAttributes();
params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER;
params.alpha = 1f;
getWindow().setAttributes(params);
}
@Override
public void onClick(View v) {
dismiss();
}
}
2.主页面代码
public class MainActivity extends AppCompatActivity {
private RocketView mRocketView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdDialog dialog = new AdDialog(this);
dialog.show();
}
}
3.布局和样式
<!--弹出对话框-->
<style name="LocatonDialogStyle" parent="@android:style/Theme.Dialog">
<item name="android:windowBackground">@android:color/transparent</item><!-- 背景透明 -->
<item name="android:windowAnimationStyle">@android:style/Theme.InputMethod</item>
</style>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/mm"
android:src="@drawable/donghomesuccess"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
改良版本
修改了下dialog的样式,添加了一个error 并且添加error的点击事件
public class AdDialog extends Dialog implements View.OnClickListener{
private Context context;
public AdDialog(@NonNull Context context) {
// 更改样式,把背景设置为透明的
super(context, R.style.LocatonDialogStyle);
this.context = context;
}
public AdDialog(@NonNull Context context, int themeResId) {
// 更改样式,把背景设置为透明的
super(context, R.style.LocatonDialogStyle);
this.context = context;
}
protected AdDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
// 更改样式,把背景设置为透明的
super(context, R.style.LocatonDialogStyle);
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加载dialog的布局
setContentView(R.layout.view_location_dialog_dong);
//拿到布局控件进行处理
ImageView imageView = (ImageView) findViewById(R.id.mm);
ImageView dia_iv = (ImageView) findViewById(R.id.dia_iv);
// imageView.setOnClickListener(this);
dia_iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dismiss();
}
});
//初始化布局的位置
initLayoutParams();
}
private void initLayoutParams() {
WindowManager.LayoutParams params = getWindow().getAttributes();
params.gravity = Gravity.CENTER_HORIZONTAL | Gravity.CENTER;
params.alpha = 1f;
getWindow().setAttributes(params);
}
@Override
public void onClick(View view) {
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="@+id/mm"
android:src="@mipmap/xj3"
android:layout_width="200dp"
android:layout_centerInParent="true"
android:layout_height="270dp"/>
<ImageView
android:id="@+id/dia_iv"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignEnd="@+id/mm"
android:layout_alignTop="@+id/mm"
app:srcCompat="@mipmap/error"/>
<!--dia_iv-->
</RelativeLayout>