DialogFragment改变大小——屏幕占比,适配每一个大小不一样的屏幕~~

为什么要使用DialogFragment?

在Activity中显示对话框或者弹出浮层时,尽量使用dialogfragment,而非必须要用dialog/AlertDialog,

这样有利于随着activity或者fragment的生命周期管理对话框。

因为它和Fragment有着基本一致的声明周期~~~ 官方也是推荐使用Dialogfragment的!!!

xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/rec_cirwhite"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="30dp"
        >

        <TextView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:gravity="center_vertical"
            android:paddingLeft="20dp"
            android:text="提示"
            android:textColor="#557dbf" />
    </RelativeLayout>

    <TextView

        android:layout_width="match_parent"
        android:layout_height="90dp"

        android:gravity="center"
        android:text="您已成功添加入库记录"
        android:textColor="#333333"
        android:textSize="15sp" />

    <TextView

        android:layout_width="match_parent"
        android:layout_height="60dp"

        android:gravity="center"
        android:text="确定"
        android:textColor="#333333"
        android:textSize="15sp" />
  
</LinearLayout>
 

rec_cirwhite xml:

<?xml version="1.0" encoding="utf-8"?>

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <corners android:radius="20dp"/>
    <solid android:color="@color/colorWhite"/>
</shape>
public class Stroage_Frgment_ok_dialog extends DialogFragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.stroage_fragment_ok_dialog, container);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        WindowManager.LayoutParams windowParams = window.getAttributes();
        windowParams.dimAmount = 0.0f;
        windowParams.y = 100;
        window.setAttributes(windowParams);
        Dialog dialog = getDialog();
        if (dialog != null) {
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.75), (int) (dm.heightPixels * 0.3));
        }
    }

1.在DialogFragment的onCreateView里面设置,可以将对话框内部的背景设为透明

扫描二维码关注公众号,回复: 3514001 查看本文章
getDialog().getWindow().setBackgroundDrawable(newColorDrawable(Color.TRANSPARENT));

2.在DialogFragment的onstart里面设置,可以将对话框外部的背景设为透明

 
  1. @Override

  2. public void onStart() {

  3. // TODO Auto-generated method stub

  4. super.onStart();

  5.  
  6. Window window = getDialog().getWindow();

  7. WindowManager.LayoutParams windowParams = window.getAttributes();

  8. windowParams.dimAmount = 0.0f;

  9.  
  10. window.setAttributes(windowParams);

  11. }

也可以使用第三方开源框架

https://github.com/Timmy-zzh/TDialog

~~~~

猜你喜欢

转载自blog.csdn.net/weixin_40350174/article/details/80939239