简体   繁体   English

警报对话框始终显示在顶部

[英]alert dialog is shown on always on top

I have an alert dialog which I would like to place in the center of my screen. 我有一个警报对话框,希望将其放置在屏幕中央。 but it always shown on top. 但它总是显示在顶部。

I have tried changing in in the xml as well as in my code but no luck. 我试图在xml中以及在我的代码中进行更改,但是没有运气。

the layout of the dialog : 对话框的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="center"
    android:layout_marginTop="50dp"
    android:minHeight="100dp"
    android:minWidth="200dp">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_weight="2"
        android:gravity="center"
        android:id="@+id/alert_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:id="@+id/alert_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FF0000"
            android:textSize="40dp"
            android:gravity="center"
            android:text="ALERT"/>
        </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_weight="3"
        android:gravity="center"
        android:layout_below="@id/alert_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="25dp"
            android:id="@+id/alert_details"
            android:text="DETAILS" />
        </LinearLayout>

</LinearLayout>

my dialog fragment : 我的对话框片段:

public class ParameterAlert extends DialogFragment implements DialogInterface.OnClickListener {
    private View form=null;
    TextView alertTitle;
    TextView alertType;
    TextView alertDetails;
    private int parameter;
    List<String> paramValues;

    public ParameterAlert(){
        //blank
    }



    @Override
    public void onResume() {
        int width=800;
        int height=600;
        super.onResume();
        Window window=getDialog().getWindow();
        window.setLayout(width, height);
        window.setGravity(Gravity.CENTER);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        getDialog().setCanceledOnTouchOutside(false);
        WindowManager.LayoutParams wmpl=getDialog().getWindow().getAttributes();
        wmpl.gravity=Gravity.CENTER_VERTICAL;

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        //form=getActivity().getLayoutInflater().inflate(R.layout.alerts,null);
        AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
        LayoutInflater inflater=getActivity().getLayoutInflater();
        View form=inflater.inflate(R.layout.alerts,null);
        builder.setView(form);

        builder.setNegativeButton(R.string.cancel_alert,this);

        Bundle bundleParam=getArguments();
        int parameter=bundleParam.getInt("parameter");
        paramValues=ReadParams.params.get(parameter);
        builder.setTitle(paramValues.get(0));
        alertTitle= (TextView) form.findViewById(R.id.alert_title);
        //alertType= (TextView) form.findViewById(R.id.alert_type);
        alertDetails= (TextView) form.findViewById(R.id.alert_details);
        //alertType.setText(paramValues.get(0));
        alertTitle.setText(paramValues.get(1));
        alertDetails.setText(paramValues.get(2));


        return builder.create();


    }


    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {

    }
}

Your outer LinerLayout has a android:layout_height="match_parent" making it full having full height thus starting from top (after the 50dp marginTop). 您的外部LinerLayout具有android:layout_height="match_parent" ,使其具有完整的高度,因此从顶部开始(在50dp marginTop之后)。

You need to change it to wrap_content . 您需要将其更改为wrap_content

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM