Today, I'll show you how to use PopupWindow.
PopupWindow enables you to display view on top of the current activity. Actually Dialog class provides similar function, but those are slightly different.
The most distinctive difference is that PopupWindow has function that displays view at anywhere you want though Dialog can not.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_count_edit_text_activity);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = (View)inflater.inflate(R.layout.popupwindow_layout, null);
popupView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
final View view = findViewById(R.id.layout_view);
final PopupWindow popupWindow = new PopupWindow(view);
popupWindow.setContentView(popupView);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
IBinder windowToken = view.getWindowToken();
if (windowToken != null && windowToken.isBinderAlive()) {
popupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER, 10, 10);
}
}
}, 1000);
}
[In Japanese]
今回はPopupWindowの使用方法について記述します。
まずPopupWindowとは、現在表示されているActivityに対し、一番上のレイヤに表示させる事ができるWindowです。Dialogでも同じような動きをしますが、Dialogとの一番の違いは、表示する位置を指定する事ができる点です。Dialogでは基本、show()メソッドを呼び出すだけで、位置の指定まではできません。
では、PopupWindowの基本的な使い方を紹介します。
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_count_edit_text_activity);
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = (View)inflater.inflate(R.layout.popupwindow_layout, null);
popupView.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
final View view = findViewById(R.id.layout_view);
final PopupWindow popupWindow = new PopupWindow(view);
popupWindow.setContentView(popupView);
popupWindow.setWidth(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
Handler mHandler = new Handler();
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
IBinder windowToken = view.getWindowToken();
if (windowToken != null && windowToken.isBinderAlive()) {
popupWindow.showAtLocation(view, Gravity.BOTTOM | Gravity.CENTER, 10, 10);
}
}
}, 1000);
}
0 件のコメント:
コメントを投稿