【AndroidのViewを制する】 PopupWindowを任意の位置に表示する
今回、紹介するPopupWindowは画面の任意の位置に好きなViewをポップアップすることができます。
メソッドがたくさんあるため数回に分割して説明します。
今回はPopupWindowを表示するものに関連するメソッドの使い方を説明します。
PopupWindow#showAsDropDown(View anchor)を使って表示する
PopupWindow#showAsDropDown(View anchor)はPopupWindowを引数で指定したViewの近くに表示することができます。
// PopupWindow内に表示するFrameLayoutを作成する。
FrameLayout contentView = new FrameLayout(this);
contentView.setBackgroundColor(Color.LTGRAY);
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow();
// PopupWindowのコンテンツにImageViewを設定する。
popupWindow.setContentView(contentView);
// PopupWindowの幅を800に設定
popupWindow.setWidth(800);
// PopupWindowの高さを800に設定
popupWindow.setHeight(800);
// showAsDropDown (View anchor)
Button showPopupWindow1 = new Button(this);
showPopupWindow1.setText("showAsDropDown (View anchor)を使って表示する");
showPopupWindow1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.showAsDropDown(v);
}
});
// PopupWindowを消すボタンを作成
Button dismissPopupWindow = new Button(this);
dismissPopupWindow.setText("PopupWindowを消す");
dismissPopupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// PopupWindow#dismissでPopupWindowを消す
popupWindow.dismiss();
}
});
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(showPopupWindow1);
linearLayout.addView(dismissPopupWindow);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(linearLayout);
setContentView(frameLayout);
PopupWindow#showAsDropDown(View anchor, int xoff, int yoff)を使って表示する
PopupWindow#showAsDropDown(View anchor, int xoff, int yoff)はPopupWindowを引数で指定したViewの近くに表示することができます。int xoffはPopupWindowをx軸方向に移動させます。正の値で右方向、負の値で左方向に移動します。
int yoffはPopupWindowをy軸方向に移動させます。正の値で下方向、負の値で上方向に移動します。
// PopupWindow内に表示するFrameLayoutを作成する。
FrameLayout contentView = new FrameLayout(this);
contentView.setBackgroundColor(Color.LTGRAY);
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow();
// PopupWindowのコンテンツにImageViewを設定する。
popupWindow.setContentView(contentView);
// PopupWindowの幅を800に設定
popupWindow.setWidth(800);
// PopupWindowの高さを800に設定
popupWindow.setHeight(800);
// showAsDropDown (View anchor, int xoff, int yoff)
Button showPopupWindow2 = new Button(this);
showPopupWindow2.setText("showAsDropDown (View anchor, int xoff, int yoff)を使って表示する");
showPopupWindow2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.showAsDropDown(v,100,100);
}
});
// PopupWindowを消すボタンを作成
Button dismissPopupWindow = new Button(this);
dismissPopupWindow.setText("PopupWindowを消す");
dismissPopupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// PopupWindow#dismissでPopupWindowを消す
popupWindow.dismiss();
}
});
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(showPopupWindow2);
linearLayout.addView(dismissPopupWindow);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(linearLayout);
setContentView(frameLayout);
PopupWindow#showAsDropDown(View anchor, int xoff, int yoff, int gravity)を使って表示する
PopupWindow#showAsDropDown(View anchor, int xoff, int yoff)はPopupWindowを引数で指定したViewの近くに表示することができます。int xoffはPopupWindowをx軸方向に移動させます。正の値で右方向、負の値で左方向に移動します。
int yoffはPopupWindowをy軸方向に移動させます。正の値で下方向、負の値で上方向に移動します。
int gravityはView anchorに対するPopupWindowの表示位置をGravityを使って設定することができます。
// PopupWindow内に表示するFrameLayoutを作成する。
FrameLayout contentView = new FrameLayout(this);
contentView.setBackgroundColor(Color.LTGRAY);
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow();
// PopupWindowのコンテンツにImageViewを設定する。
popupWindow.setContentView(contentView);
// PopupWindowの幅を800に設定
popupWindow.setWidth(800);
// PopupWindowの高さを800に設定
popupWindow.setHeight(800);
// showAsDropDown (View anchor, int xoff, int yoff, int gravity)
Button showPopupWindow3 = new Button(this);
showPopupWindow3.setText("showAsDropDown (View anchor, int xoff, int yoff, int gravity)を使って表示する");
showPopupWindow3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.showAsDropDown(v,100,100, Gravity.RIGHT);
}
});
// PopupWindowを消すボタンを作成
Button dismissPopupWindow = new Button(this);
dismissPopupWindow.setText("PopupWindowを消す");
dismissPopupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// PopupWindow#dismissでPopupWindowを消す
popupWindow.dismiss();
}
});
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(showPopupWindow3);
linearLayout.addView(dismissPopupWindow);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(linearLayout);
setContentView(frameLayout);
PopupWindow#showAtLocation(View parent,int gravity, int x, int y)を使って表示する
PopupWindow#showAtLocation(View parent,int gravity, int x, int y)は画面の任意の位置にPopupWindowを表示することができます。int gravityはPopupWindowの表示位置をGravityを使って設定することができます。
int xはPopupWindowをx軸方向に移動させます。正の値で右方向、負の値で左方向に移動します。
int yはPopupWindowをy軸方向に移動させます。正の値で下方向、負の値で上方向に移動します。
// PopupWindow内に表示するFrameLayoutを作成する。
FrameLayout contentView = new FrameLayout(this);
contentView.setBackgroundColor(Color.LTGRAY);
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow();
// PopupWindowのコンテンツにImageViewを設定する。
popupWindow.setContentView(contentView);
// PopupWindowの幅を800に設定
popupWindow.setWidth(800);
// PopupWindowの高さを800に設定
popupWindow.setHeight(800);
// showAtLocation (View parent,int gravity, int x, int y)
Button showPopupWindow4 = new Button(this);
showPopupWindow4.setText("showAtLocation (View parent,int gravity, int x, int y)を使って表示する");
showPopupWindow4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.showAtLocation(v,Gravity.CENTER,-100,-100);
}
});
// PopupWindowを消すボタンを作成
Button dismissPopupWindow = new Button(this);
dismissPopupWindow.setText("PopupWindowを消す");
dismissPopupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// PopupWindow#dismissでPopupWindowを消す
popupWindow.dismiss();
}
});
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(showPopupWindow4);
linearLayout.addView(dismissPopupWindow);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(linearLayout);
setContentView(frameLayout);
PopupWindow#isShowingを使って表示状態を確認する
PopupWindow#isShowingはPopupWindowが表示されているかどうかを返却します。PopupWindowが表示されている場合はtrueを、表示されていない場合はfalseを返却します。
// PopupWindow内に表示するFrameLayoutを作成する。
FrameLayout contentView = new FrameLayout(this);
contentView.setBackgroundColor(Color.LTGRAY);
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow();
// PopupWindowのコンテンツにImageViewを設定する。
popupWindow.setContentView(contentView);
// PopupWindowの幅を800に設定
popupWindow.setWidth(800);
// PopupWindowの高さを800に設定
popupWindow.setHeight(800);
// showAsDropDown (View anchor)
Button showPopupWindow1 = new Button(this);
showPopupWindow1.setText("showAsDropDown (View anchor)を使って表示する");
showPopupWindow1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.showAsDropDown(v);
}
});
// PopupWindowを消すボタンを作成
Button dismissPopupWindow = new Button(this);
dismissPopupWindow.setText("PopupWindowを消す");
dismissPopupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// PopupWindow#dismissでPopupWindowを消す
popupWindow.dismiss();
}
});
// PopupWindowの表示状態を確認するボタンを作成
Button isShowingPopupWindow = new Button(this);
isShowingPopupWindow.setText("PopupWindowの表示状態を確認する");
isShowingPopupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(PopupWindowActivity.this, popupWindow.isShowing() ? "表示されています。" : "表示されていません", Toast.LENGTH_SHORT).show();
}
});
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(showPopupWindow1);
linearLayout.addView(dismissPopupWindow);
linearLayout.addView(isShowingPopupWindow);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(linearLayout);
setContentView(frameLayout);
PopupWindow#isAboveAnchorを使って表示位置を確認する
PopupWindow#isAboveAnchorはPopupWindowが指定したViewの上に表示されているかどうかを返却します。PopupWindowがViewの上に表示されている場合はtrueを、下に表示されていない場合はfalseを返却します。
// PopupWindow内に表示するFrameLayoutを作成する。
FrameLayout contentView = new FrameLayout(this);
contentView.setBackgroundColor(Color.LTGRAY);
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow();
// PopupWindowのコンテンツにImageViewを設定する。
popupWindow.setContentView(contentView);
// PopupWindowの幅を800に設定
popupWindow.setWidth(800);
// PopupWindowの高さを800に設定
popupWindow.setHeight(800);
// PopupWindowを消すボタンを作成
Button dismissPopupWindow = new Button(this);
dismissPopupWindow.setText("PopupWindowを消す");
dismissPopupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// PopupWindow#dismissでPopupWindowを消す
popupWindow.dismiss();
}
});
// PopupWindowの表示状態を確認するボタンを作成
Button isAboveAnchorPopupWindow = new Button(this);
isAboveAnchorPopupWindow.setText("PopupWindowの表示が上か下かを確認する");
isAboveAnchorPopupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(PopupWindowActivity2.this, popupWindow.isAboveAnchor() ? "上に表示されています。" : "下に表示されています。", Toast.LENGTH_SHORT).show();
}
});
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.addView(dismissPopupWindow);
linearLayout.addView(isAboveAnchorPopupWindow);
for (int i = 1; i < 20; i++) {
// showAsDropDown (View anchor)
Button showPopupWindow = new Button(this);
showPopupWindow.setText("表示ボタン : " + i);
showPopupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.showAsDropDown(v);
}
});
linearLayout.addView(showPopupWindow);
}
ScrollView scrollView = new ScrollView(this);
scrollView.addView(linearLayout);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(scrollView);
setContentView(frameLayout);