設置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定義---------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout"
android:layout_width="@dimen/px_375.0"
android:layout_height="@dimen/px_300.0"
android:background="@drawable/pin_dialog_background"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3.0"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_prompt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textColor="@color/black" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1.5"
android:orientation="horizontal">
<Button
android:id="@+id/btn_ok"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="@drawable/selector_button"
android:gravity="center"
android:text="@string/ok"
android:textColor="@color/white" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.0"
android:background="@drawable/selector_button"
android:gravity="center"
android:text="@string/cancel"
android:textColor="@color/white" />
</LinearLayout>
</LinearLayout>
-----------------------------------顯示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