Android里设置Dialog位置

                                                   设置Dialog位置

      Dialog弹出的位置默认为屏幕的中间位置,那怎么改变它的弹出问题呢? 

      上代码先。

         ------------------------------------------dialog的定义----------------------------------

package com.amlogic.dvb_dialog;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.LinearLayout;

import com.amlogic.DVBPlayer.R;

public class SureDialog extends Dialog {
    private Context mContext;
    private LinearLayout mll;

    public SureDialog(Context context, int theme) {
        super(context, theme);

        mContext = context;

        View inflate = LayoutInflater.from(mContext).inflate(R.layout.simple_save_dialog, null);
        setContentView(inflate);

        mll = findViewById(R.id.layout);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    public void show() {
        super.show();

        mll.post(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                startOnCreateAnimator();
            }
        });
    }

    //动画效果
    private void startOnCreateAnimator() {
        float TranslationY = mll.getTranslationY();
        float start_position = TranslationY - mll.getHeight();
        ObjectAnimator transAnim1 = ObjectAnimator.ofFloat(mll, "translationY", start_position, TranslationY);
        transAnim1.setInterpolator(new AccelerateInterpolator());
        transAnim1.setDuration(400);
        transAnim1.start();
    }
}
            ------------------------------------------xml定义---------------------------------




    

        
    

    

        
                 -----------------------------------显示dialog------------------------------

private void showFactorySureDialog() {
        SureDialog mSureDialog = new SureDialog(getContext(), R.style.dialog_sen5_01);

        Button btn_ok = (Button) mSureDialog.findViewById(R.id.btn_ok);
        Button btn_cancel = (Button) mSureDialog.findViewById(R.id.btn_cancel);
        TextView tv_prompt = (TextView) mSureDialog.findViewById(R.id.tv_prompt);

        tv_prompt.setText(R.string.factory_reset);

        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSureDialog.dismiss();
        });

        btn_cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mSureDialog.dismiss();

            }
        });

        Rect rect = getFragmentCenterLocationOnScreen();
        WindowManager.LayoutParams params = mSureDialog.getWindow().getAttributes();

        //一定要在这里设置dialog的高和宽,否则高和宽的值是不确定的。
        params.width = (int) getContext().getResources().getDimension(R.dimen.px_450_0);
        params.height = (int) getContext().getResources().getDimension(R.dimen.px_240_0);

        params.gravity = Gravity.LEFT | Gravity.TOP;
        params.x = rect.left - params.width/2;
        params.y = rect.top - params.height/2;
        mSureDialog.getWindow().setAttributes(params);

        mSureDialog.show();
    }

getFragmentCenterLocationOnScreen()就是获取dialog的位置信息的。

private Rect getFragmentCenterLocationOnScreen() {
        int[] location = new int[2];
        Rect rect;
        mRlParent.getLocationOnScreen(location);
        int fragment_width = mRlParent.getWidth();
        int fragment_height = mRlParent.getHeight();

        location[0] = location[0] + fragment_width/2;
        location[1] = location[1] + fragment_height/2;

        rect = new Rect(location[0], location[1], location[0] + fragment_width, location[1] + fragment_height);
        return rect;
    }

我这里的意思就是dialog要显示在自定义的fragment的中间位置。最根本的方法就是设置params.gravity、params.x和params.y。

 

                                                                                    THE     END

 

你可能感兴趣的:(android)