自定义全屏Dialog
2017-04-17 本文已影响586人
Y小圆脸
需求:自定义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="match_parent"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/rl_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:visibility="visible">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:orientation="vertical">
<ImageView
android:paddingBottom="32dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/group" />
<Button
android:id="@+id/btn_use"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="@drawable/btn_bg"
android:text="立即使用"
android:textColor="@color/white"
android:textSize="@dimen/dm_18" />
<ImageView
android:id="@+id/iv_dismss"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingBottom="35dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:paddingTop="35dp"
android:src="@drawable/dismiss" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
java代码
package com.cfiigroup.zhehui.hepaidai.ui.dialog;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class PromptDialog extends DialogFragment implements View.OnClickListener {
private Button mBtnUse;
private ImageView mIvDismss;
private RelativeLayout mRlActivity;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return super.onCreateDialog(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
getDialog().setCanceledOnTouchOutside(false);
setCancelable(true);
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.color_dialog_bg)));
View v = inflater.inflate(R.layout.dialog_customview, null);
initView(v);
return v;
}
@Override
public void onStart() {
super.onStart();
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
}
private void initView(View v) {
mBtnUse = (Button) v.findViewById(R.id.btn_use);
mIvDismss = (ImageView) v.findViewById(R.id.iv_dismss);
mRlActivity = (RelativeLayout) v.findViewById(R.id.rl_activity);
mBtnUse.setOnClickListener(this);
mIvDismss.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_use:
UiManager.callWebActivity(getActivity(), UiManager.webflag_tiyanjin);
dismiss();
break;
case R.id.iv_dismss:
dismiss();
break;
}
}
}
分析:定义全屏dailog window去除title 设置主题使其全屏
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
设置背景透明度
getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(getResources().getColor(R.color.color_dialog_bg)));