弹出窗口的位置 activity
Location for pop up activity
我有一个 activity 想要显示为弹出窗口 window。
我有以下代码,但 activity 显示在中间。我想在不同的位置展示它。可能在我的工具栏下面。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup_message);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = 120;
getWindow().setLayout((int) (width * .98), height);
您需要获取当前视图 window 并设置该视图的 x 和 y 参数 window。所以它将设置在所需的位置。
以下是我如何在所需位置设置对话框的示例。
Window window = getWindow();
// set "origin" to top left corner, so to speak
window.setGravity(Gravity.TOP | Gravity.START);
// after that, setting values for x and y works "naturally"
WindowManager.LayoutParams params = window.getAttributes();
params.x =selectedViewWidth;
params.y = selectedViewHeight/2;
window.setAttributes(params);
I personally think that if you want to show popup window then instead of using activity you can also use PopupWindow
or DialogFragment
. You can manage by this controls more effectively.
There are so many tutorials available for same.
我有一个 activity 想要显示为弹出窗口 window。
我有以下代码,但 activity 显示在中间。我想在不同的位置展示它。可能在我的工具栏下面。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_popup_message);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = 120;
getWindow().setLayout((int) (width * .98), height);
您需要获取当前视图 window 并设置该视图的 x 和 y 参数 window。所以它将设置在所需的位置。
以下是我如何在所需位置设置对话框的示例。
Window window = getWindow();
// set "origin" to top left corner, so to speak
window.setGravity(Gravity.TOP | Gravity.START);
// after that, setting values for x and y works "naturally"
WindowManager.LayoutParams params = window.getAttributes();
params.x =selectedViewWidth;
params.y = selectedViewHeight/2;
window.setAttributes(params);
I personally think that if you want to show popup window then instead of using activity you can also use
PopupWindow
orDialogFragment
. You can manage by this controls more effectively. There are so many tutorials available for same.