【AndroidのViewを制する】 表示中のPopupWindowを更新する
今回、紹介するPopupWindowは画面の任意の位置に好きなViewをポップアップすることができます。
メソッドがたくさんあるため数回に分割して説明します。
今回は表示されているPopupWindowを更新するメソッドの使い方を説明します。
PopupWindow#update()を使って設定を更新する
PopupWindowが表示されている状態で設定を変更するメソッドを使用してもPopupWindowには影響がありません。PopupWindow#update()は最新の設定をPopupWindowに適用するメソッドです。
- setClippingEnabled(boolean)
- setFocusable(boolean)
- setIgnoreCheekPress()
- setInputMethodMode(int)
- setTouchable(boolean)
- setAnimationStyle(int)
// PopupWindow内に表示するFrameLayoutを作成する。
FrameLayout contentView = new FrameLayout(this);
contentView.setBackgroundColor(Color.RED);
// PopupWindowをクリックするとトーストが表示されるように設定
contentView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(PopupWindowActivity3.this, "contentViewがクリックされました", Toast.LENGTH_SHORT).show();
}
});
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow();
// PopupWindowのコンテンツにImageViewを設定する。
popupWindow.setContentView(contentView);
// PopupWindowの幅を500に設定
popupWindow.setWidth(500);
// PopupWindowの高さを500に設定
popupWindow.setHeight(500);
// PopupWindowがタッチできるように設定
popupWindow.setTouchable(true);
// showAsDropDown (View anchor)
Button showPopupWindow = new Button(this);
showPopupWindow.setText("PopupWindowを表示する");
showPopupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// PopupWindowの幅を500に設定
popupWindow.setWidth(500);
// PopupWindowの高さを500に設定
popupWindow.setHeight(500);
// PopupWindowがタッチできるように設定
popupWindow.setTouchable(true);
popupWindow.showAsDropDown(v,500,500);
}
});
// update()
Button updatePopupWindow1 = new Button(this);
updatePopupWindow1.setText("update()を使って設定を変更する");
updatePopupWindow1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// PopupWindowがタッチできないように設定
popupWindow.setTouchable(false);
// 変更したPopupWindowの設定を反映する
popupWindow.update();
}
});
// 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(dismissPopupWindow);
linearLayout.addView(showPopupWindow);
linearLayout.addView(updatePopupWindow1);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(linearLayout);
setContentView(frameLayout);
PopupWindow#update(int width, int height)を使って幅と高さを更新する
PopupWindow#update(int width, int height)は最新の設定をPopupWindowに適用するメソッドです。int widthは幅を設定します。負の値を設定すると無視されます、高さだけを設定したい場合は-1を設定してください。
int heightは高さを設定します。負の値を設定すると無視されます、幅だけを設定したい場合は-1を設定してください。
// PopupWindow内に表示するFrameLayoutを作成する。
FrameLayout contentView = new FrameLayout(this);
contentView.setBackgroundColor(Color.RED);
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow();
// PopupWindowのコンテンツにImageViewを設定する。
popupWindow.setContentView(contentView);
// PopupWindowの幅を500に設定
popupWindow.setWidth(500);
// PopupWindowの高さを500に設定
popupWindow.setHeight(500);
// showAsDropDown (View anchor)
Button showPopupWindow = new Button(this);
showPopupWindow.setText("PopupWindowを表示する");
showPopupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// PopupWindowの幅を500に設定
popupWindow.setWidth(500);
// PopupWindowの高さを500に設定
popupWindow.setHeight(500);
popupWindow.showAsDropDown(v,500,500);
}
});
// update(int width, int height)
Button updatePopupWindow2 = new Button(this);
updatePopupWindow2.setText("update(int width, int height)を使って表示を変更する");
updatePopupWindow2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.update(600,600);
}
});
// 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(dismissPopupWindow);
linearLayout.addView(showPopupWindow);
linearLayout.addView(updatePopupWindow2);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(linearLayout);
setContentView(frameLayout);
PopupWindow#update(View anchor, int width, int height)を使ってアンカーを更新する
PopupWindow#update(View anchor, int width, int height)はPopupWindowの表示位置を任意Viewの近くに更新するメソッドです。View anchorはPopupWindowを表示するViewを設定します。
int widthは幅を設定します。負の値を設定すると無視されます、幅をを変更したくない場合は-1を設定してください。
int heightは高さを設定します。負の値を設定すると無視されます、高さを変更したくない場合は-1を設定してください。
// PopupWindow内に表示するFrameLayoutを作成する。
FrameLayout contentView = new FrameLayout(this);
contentView.setBackgroundColor(Color.RED);
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow();
// PopupWindowのコンテンツにImageViewを設定する。
popupWindow.setContentView(contentView);
// PopupWindowの幅を500に設定
popupWindow.setWidth(500);
// PopupWindowの高さを500に設定
popupWindow.setHeight(500);
// showAsDropDown (View anchor)
Button showPopupWindow = new Button(this);
showPopupWindow.setText("PopupWindowを表示する");
showPopupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// PopupWindowの幅を500に設定
popupWindow.setWidth(500);
// PopupWindowの高さを500に設定
popupWindow.setHeight(500);
popupWindow.showAsDropDown(v,500,500);
}
});
// update(View anchor, int width, int height)
Button updatePopupWindow3 = new Button(this);
updatePopupWindow3.setText("update(View anchor, int width, int height)を使って表示を変更する");
updatePopupWindow3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.update(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(dismissPopupWindow);
linearLayout.addView(showPopupWindow);
linearLayout.addView(updatePopupWindow3);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(linearLayout);
setContentView(frameLayout);
PopupWindow#update(View anchor, int xoff, int yoff, int width, int height)を使ってアンカーを更新する
PopupWindow#update(View anchor, int xoff, int yoff, int width, int height)はPopupWindowの表示位置を任意Viewの近くに更新するメソッドです。View anchorはPopupWindowを表示するViewを設定します。
int xoffはPopupWindowの左端とアンカーViewの左端の距離を設定します。
int yoffはPopupWindowの上端とアンカーViewの下端の距離を設定します。
int widthは幅を設定します。負の値を設定すると無視されます、幅を変更したくない場合は-1を設定してください。
int heightは高さを設定します。負の値を設定すると無視されます、高さを変更したくない場合は-1を設定してください。
// PopupWindow内に表示するFrameLayoutを作成する。
FrameLayout contentView = new FrameLayout(this);
contentView.setBackgroundColor(Color.RED);
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow();
// PopupWindowのコンテンツにImageViewを設定する。
popupWindow.setContentView(contentView);
// PopupWindowの幅を500に設定
popupWindow.setWidth(500);
// PopupWindowの高さを500に設定
popupWindow.setHeight(500);
// showAsDropDown (View anchor)
Button showPopupWindow = new Button(this);
showPopupWindow.setText("PopupWindowを表示する");
showPopupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// PopupWindowの幅を500に設定
popupWindow.setWidth(500);
// PopupWindowの高さを500に設定
popupWindow.setHeight(500);
popupWindow.showAsDropDown(v,500,500);
}
});
// update(View anchor, int xoff, int yoff, int width, int height)
Button updatePopupWindow4 = new Button(this);
updatePopupWindow4.setText("update(View anchor, int xoff, int yoff, int width, int height)を使って表示をする");
updatePopupWindow4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.update(v,100,-400,200,200);
}
});
// 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(dismissPopupWindow);
linearLayout.addView(showPopupWindow);
linearLayout.addView(updatePopupWindow4);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(linearLayout);
setContentView(frameLayout);
PopupWindow#update(int x, int y, int width, int height)を使って表示位置を更新する
PopupWindow#update(int x, int y, int width, int height)はPopupWindowの表示位置を画面上の任意の位置にを更新するメソッドです。int xはPopupWindowの左端と画面の左端の距離を設定します。
int yはPopupWindowの上端と画面の上端の距離を設定します。
int widthは幅を設定します。負の値を設定すると無視されます、幅を変更したくない場合は-1を設定してください。
int heightは高さを設定します。負の値を設定すると無視されます、高さを変更したくない場合は-1を設定してください。
// PopupWindow内に表示するFrameLayoutを作成する。
FrameLayout contentView = new FrameLayout(this);
contentView.setBackgroundColor(Color.RED);
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow();
// PopupWindowのコンテンツにImageViewを設定する。
popupWindow.setContentView(contentView);
// PopupWindowの幅を500に設定
popupWindow.setWidth(500);
// PopupWindowの高さを500に設定
popupWindow.setHeight(500);
// showAsDropDown (View anchor)
Button showPopupWindow = new Button(this);
showPopupWindow.setText("PopupWindowを表示する");
showPopupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// PopupWindowの幅を500に設定
popupWindow.setWidth(500);
// PopupWindowの高さを500に設定
popupWindow.setHeight(500);
popupWindow.showAsDropDown(v,500,500);
}
});
// update(int x, int y, int width, int height)
Button updatePopupWindow5 = new Button(this);
updatePopupWindow5.setText("update(int x, int y, int width, int height)を使って表示を変更する");
updatePopupWindow5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.update(200,200,200,200);
}
});
// 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(dismissPopupWindow);
linearLayout.addView(showPopupWindow);
linearLayout.addView(updatePopupWindow5);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(linearLayout);
setContentView(frameLayout);
PopupWindow#update(int x, int y, int width, int height, boolean force)を使って表示位置を更新する
PopupWindow#update(int x, int y, int width, int height, boolean force)はPopupWindowの表示位置を画面上の任意の位置にを更新するメソッドです。int xはPopupWindowの左端と画面の左端の距離を設定します。
int yはPopupWindowの上端と画面の上端の距離を設定します。
int widthは幅を設定します。負の値を設定すると無視されます、幅を変更したくない場合は-1を設定してください。
int heightは高さを設定します。負の値を設定すると無視されます、高さを変更したくない場合は-1を設定してください。
boolean forceは位置x,yに対してPopupWindowのLayoutParamsが対応しているしている場合でも強制的に位置を変更する場合はtrue、 必要に応じて再配置する場合はfalseを設定します。
// PopupWindow内に表示するFrameLayoutを作成する。
FrameLayout contentView = new FrameLayout(this);
contentView.setBackgroundColor(Color.RED);
// PopupWindowのインスタンスを作成する
final PopupWindow popupWindow = new PopupWindow();
// PopupWindowのコンテンツにImageViewを設定する。
popupWindow.setContentView(contentView);
// PopupWindowの幅を500に設定
popupWindow.setWidth(500);
// PopupWindowの高さを500に設定
popupWindow.setHeight(500);
// showAsDropDown (View anchor)
Button showPopupWindow = new Button(this);
showPopupWindow.setText("PopupWindowを表示する");
showPopupWindow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// PopupWindowの幅を500に設定
popupWindow.setWidth(500);
// PopupWindowの高さを500に設定
popupWindow.setHeight(500);
popupWindow.showAsDropDown(v,500,500);
}
});
// update(int x, int y, int width, int height, boolean force)
Button updatePopupWindow6 = new Button(this);
updatePopupWindow6.setText("update(int x, int y, int width, int height, boolean force)を使って表示を変更する");
updatePopupWindow6.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
popupWindow.update(100,100,500,500,true);
}
});
// 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(dismissPopupWindow);
linearLayout.addView(showPopupWindow);
linearLayout.addView(updatePopupWindow6);
FrameLayout frameLayout = new FrameLayout(this);
frameLayout.addView(linearLayout);
setContentView(frameLayout);