【AndroidのViewを制する】 PopupWindowを使いこなしてポップアップを表示する
今回、紹介するPopupWindowは画面の任意の位置に好きなViewをポップアップすることができます。
メソッドがたくさんあるため数回に分割して説明します。
今回はPopupWindowの基本的な使い方を説明します。
PopupWindowのコンストラクタ
PopupWindowにはたくさんのコンストラクタが存在しています。|
PopupWindow() PopupWindow(Context context) PopupWindow(Context context, AttributeSet attrs) PopupWindow(Context context, AttributeSet attrs, int defStyleAttr) PopupWindow(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) |
表示するViewが設定されていないPopupWindowを作成します。 これらのコンストラクタで作成した場合、PopupWindowはフォーカスが当たらないかつ、幅と高さが0の状態です。 |
| PopupWindow(int width, int height) | 表示するViewが設定されていない、幅と高さが設定されているPopupWindowを作成します。 このコンストラクタで作成した場合、PopupWindowはフォーカスが当たらない状態です。 |
| PopupWindow(View contentView) | 表示するViewが設定されたPopupWindowを作成します。 このコンストラクタで作成した場合、PopupWindowはフォーカスが当たらないかつ、幅と高さが0の状態です。 また、PopupWindowには背景が設定されていないため別途設定する必要があります。 |
| PopupWindow(View contentView, int width, int height) | 表示するViewと背景の幅と高さが設定されたPopupWindowを作成します。 このコンストラクタで作成した場合、PopupWindowはフォーカスが当たらない状態です。 |
| PopupWindow(View contentView, int width, int height, boolean focusable) | 表示するViewと背景の幅と高さ、フォーカスが当たるかどうかが設定されたPopupWindowを作成します。 |
PopupWindow#setContentViewを使ってPopupWindow内に表示されるViewを設定する
PopupWindow#setContentViewはPopupWindow内に表示するViewを設定することができます。PopupWindowが表示されている場合はViewの設定はされません。
PopupWindow#getContentViewはPopupWindow内に表示されるViewを返却されます。
// PopupWindow内に表示するFrameLayoutを作成する。
FrameLayout contentView = new FrameLayout(this);
contentView.setBackgroundColor(Color.LTGRAY);
// PopupWindowのインスタンスを作成する
PopupWindow popupWindow = new PopupWindow();
// PopupWindow内に表示するViewを設定する。
popupWindow.setContentView(contentView);
PopupWindow#setWidthとPopupWindow#setHeightを使ってPopupWindowの幅と高さを設定する
PopupWindow#setWidthはPopupWindowの幅を設定します。PopupWindow#setHeightはPopupWindowの高さを設定します。
PopupWindow#getWidthはPopupWindowの幅を返却します。
PopupWindow#getHeightはPopupWindowの高さを返却します。
PopupWindow#setContentViewで設定されたViewは、上記のメソッドで設定された幅と高さに再設定されます。
そのため、複雑なViewを表示するならFrameLayout等のViewGroupをPopupWindow#setContentViewで設定すべきです。
// PopupWindow内に表示するFrameLayoutを作成する。
FrameLayout contentView = new FrameLayout(this);
contentView.setBackgroundColor(Color.LTGRAY);
// PopupWindowのインスタンスを作成する
PopupWindow popupWindow = new PopupWindow();
// PopupWindow内に表示するViewを設定する。
popupWindow.setContentView(contentView);
// PopupWindowの幅を800に設定
popupWindow.setWidth(800);
// PopupWindowの高さを800に設定
popupWindow.setHeight(800);
PopupWindow#showAsDropDownを使ってPopupWindowを表示する
PopupWindowを表示するためのメソッドが多数あります。今回は任意のViewの近くに表示するPopupWindow#showAsDropDownを説明します。
PopupWindow#isShowingは現在PopupWindowが表示されているかどうかを返却します。
他のメソッドについては次回説明します。
// PopupWindow内に表示するFrameLayoutを作成する。
FrameLayout contentView = new FrameLayout(this);
contentView.setBackgroundColor(Color.LTGRAY);
// PopupWindowのインスタンスを作成する
PopupWindow popupWindow = new PopupWindow();
// PopupWindow内に表示するViewを設定する。
popupWindow.setContentView(contentView);
// PopupWindowの幅を800に設定
popupWindow.setWidth(800);
// PopupWindowの高さを800に設定
popupWindow.setHeight(800);
// PopupWindowを表示するボタンを作成
Button showPopupWindow = new Button(this);
showPopupWindow.setText("PopupWindowを表示する");
showPopupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// PopupWindow#showAsDropDownでPopupWindowを表示する
popupWindow.showAsDropDown(v);
}
});
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.addView(showPopupWindow);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(linearLayout);
setContentView(frameLayout);
PopupWindow#dismissを使ってPopupWindowを非表示にする
PopupWindow#dismissはPopupWindowを非表示にするメソッドです。
// PopupWindow内に表示するFrameLayoutを作成する。
FrameLayout contentView = new FrameLayout(this);
contentView.setBackgroundColor(Color.LTGRAY);
// PopupWindowのインスタンスを作成する
PopupWindow popupWindow = new PopupWindow();
// PopupWindow内に表示するViewを設定する。
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();
}
});
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.addView(dismissPopupWindow);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(linearLayout);
setContentView(frameLayout);