关键代码:
Dialog全屏需要关键代码就是:
lp.width = WindowManager.LayoutParams.FILL_PARENT; //设置为屏幕宽度
lp.height = WindowManager.LayoutParams.FILL_PARENT; //设置为屏幕高度
效果图:
完整代码:
ReminderDialog.java:
package com.lemonmarket.ui.view;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import com.lemonmarket.MainActivity;
import com.lemonmarket.R;
import com.lemonmarket.util.SPUtils;
/**
* 首页 - 温馨提示
*/
public class ReminderDialog extends Dialog implements View.OnClickListener {
private Context context;
private TextView tv_body;
private TextView tv_refuse;
private TextView tv_agree;
public ReminderDialog(@NonNull Context context) {
super(context, R.style.dialogTransparent);
this.context = context;
}
public ReminderDialog(@NonNull Context context, int themeResId) {
super(context, themeResId);
}
protected ReminderDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener) {
super(context, cancelable, cancelListener);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_reminder);
Window dialogWindow = getWindow();
WindowManager.LayoutParams lp = dialogWindow.getAttributes();
lp.width = WindowManager.LayoutParams.FILL_PARENT; //设置为屏幕宽度
lp.height = WindowManager.LayoutParams.FILL_PARENT; //设置为屏幕高度
lp.dimAmount = 0.3f;//外围遮罩透明度0.0f-1.0f
dialogWindow.setAttributes(lp);
dialogWindow.getDecorView().setPadding(0, 0, 0, 0); //设置边距
dialogWindow.setGravity(Gravity.TOP);//内围区域底部显示
tv_body = findViewById(R.id.tv_body);
tv_refuse = findViewById(R.id.tv_refuse);
tv_agree = findViewById(R.id.tv_agree);
tv_refuse.setOnClickListener(this);
tv_agree.setOnClickListener(this);
initData();
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv_refuse:
((MainActivity) context).finish();
break;
case R.id.tv_agree:
SPUtils.setParam(context, SPUtils.SP_REMINDER, false);
dismiss();
break;
}
}
private void initData() {
String bodyText = "我们可能会收集、保存和使用下列与您有关的信息才能实现上述这些功能。如果您不提供相关信息,您将无法享受我们提供的产品与/或服务。这些功能包括:\n" +
"以上采集支付信遵循“业务必须”和“最小化”原则,不收集与所提供服务无关的支付信息。";
tv_body.setText(bodyText);
}
}
Styles.xml:
<resources>
<style name="dialogTransparent" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item> <!--边框-->
<item name="android:windowIsFloating">true</item> <!--是否浮现在activity之上-->
<item name="android:windowIsTranslucent">true</item> <!--半透明-->
<item name="android:windowNoTitle">true</item> <!--无标题-->
<item name="android:background">@android:color/transparent</item> <!--背景透明-->
<item name="android:windowBackground">@android:color/transparent</item> <!--背景透明-->
<item name="android:backgroundDimEnabled">true</item> <!--模糊-->
<item name="android:backgroundDimAmount">0.6</item> <!--背景透明度-->
</style>
</resources>
dialog_reminder.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="温馨提示"
android:textColor="@color/font_title_top"
android:textSize="18sp"
android:gravity="center"
android:padding="15dp"/>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/line_EDEDED"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:layout_marginRight="10dp"
android:text="我们依据最新的监管要求更新《隐私政策》,特向您说明如下:"
android:textColor="@color/black"/>
<TextView
android:id="@+id/tv_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="50dp"
android:lineSpacingMultiplier="1.5"
android:textColor="@color/font_65"/>
</LinearLayout>
</ScrollView>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="@color/line_EDEDED"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_refuse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="不同意"
android:padding="15dp"
android:textSize="12sp"
android:textColor="@color/font_FE5354"
android:gravity="center"
android:background="@drawable/bg_confirm_null"/>
<TextView
android:id="@+id/tv_agree"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:text="同意"
android:padding="15dp"
android:textSize="12sp"
android:textColor="@color/white"
android:gravity="center"
android:background="@drawable/bg_confirm_yes"/>
</LinearLayout>
</LinearLayout>
调用:
new ReminderDialog(this).show();
备注:
布局文件中的色值和背景没有放