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

时间:2015-05-10 13:42:45

标签: android alertdialog

我有一个警告对话框,我想将其放在屏幕中央。但它始终显示在顶部。

我尝试过更换xml以及我的代码,但没有运气。

对话框的布局:

<?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>

我的对话框片段:

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) {

    }
}

0 个答案:

没有答案