23

フラグメント内のビューをクリックすると表示されるポップアップ ウィンドウのようなものを作成しようとしています。ダイアログフラグメントのようにフラグメントを暗くしないように、このポップアップウィンドウか何かが必要です。また、ビューがクリックされた場所にポップアップを配置したいと考えています。独自のアクティビティとレイアウトがあれば、カスタム変更を加えることができます。サンプルコードを教えてください。

4

1 に答える 1

51

以下は、仕様に従って完全に機能するはずです。onClick(View v)ビューにOnClickListener割り当てられた内部からこのメソッドを呼び出します。

public void showPopup(View anchorView) {

    View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);

    PopupWindow popupWindow = new PopupWindow(popupView, 
                           LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    // Example: If you have a TextView inside `popup_layout.xml`    
    TextView tv = (TextView) popupView.findViewById(R.id.tv);

    tv.setText(....);

    // Initialize more widgets from `popup_layout.xml`
    ....
    ....

    // If the PopupWindow should be focusable
    popupWindow.setFocusable(true);

    // If you need the PopupWindow to dismiss when when touched outside 
    popupWindow.setBackgroundDrawable(new ColorDrawable());

    int location[] = new int[2];

    // Get the View's(the one that was clicked in the Fragment) location
    anchorView.getLocationOnScreen(location);

    // Using location, the PopupWindow will be displayed right under anchorView
    popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, 
                                     location[0], location[1] + anchorView.getHeight());

}

コメントはこれを十分に説明する必要があります。からanchorViewです。vonClick(View v)

于 2013-08-27T09:56:34.753 に答える