0

スワイプビューの1つ(Fragmentを拡張するLayoutTwoクラス)からポップアップを開こうとしています。以下のコードを持っていますが、ボタンを押しても何も起こりません。ビューの受け渡しに問題があると思いますが、よくわかりません。何かアイデアはありますか?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    root = (ViewGroup) inflater.inflate(R.layout.layout_two, null); 
    this.container = container;
    Button test = (Button) root.findViewById(R.id.bButton);
    test.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

            initiatePopupWindow();

        }
    });


    return root;
}
private void initiatePopupWindow() {
    try {

        LayoutInflater inflater = (LayoutInflater) container.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View layout = inflater.inflate(R.layout.popup_layout,
                (ViewGroup) container.findViewById(R.id.popup_element));

        pw = new PopupWindow(layout, 300, 470, true);

        pw.showAtLocation(layout, Gravity.CENTER, 0, 0);

        TextView mResultText = (TextView) layout.findViewById(R.id.server_status_text);
        Button cancelButton = (Button) layout.findViewById(R.id.end_data_send_button);

        cancelButton.setOnClickListener(cancel_button_click_listener);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

編集

public class LayoutTwo extends Fragment {

    private PopupWindow pw;
    private ViewGroup root;
    private Context C;
    private ViewGroup container;


    public static Fragment newInstance(Context context) {
        LayoutTwo f = new LayoutTwo();  

        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        root = (ViewGroup) inflater.inflate(R.layout.layout_two, null); 
        this.container = container;
        Button test = (Button) root.findViewById(R.id.bButton);
        test.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                initiatePopupWindow();

            }
        });


        return root;
    }
    private void initiatePopupWindow() {
        try {

            LayoutInflater inflater = (LayoutInflater) container.getContext()
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View layout = inflater.inflate(R.layout.popup_layout,
                    (ViewGroup) container.findViewById(R.id.popup_element));
            View mainLayout = container.findViewById(R.id.linear);
            pw = new PopupWindow(mainLayout, 300, 470, true);

            pw.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);

            TextView mResultText = (TextView) layout.findViewById(R.id.server_status_text);
            Button cancelButton = (Button) layout.findViewById(R.id.end_data_send_button);

            cancelButton.setOnClickListener(cancel_button_click_listener);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private OnClickListener cancel_button_click_listener = new OnClickListener() {
        public void onClick(View v) {
            pw.dismiss();
        }
    };

}
4

1 に答える 1

1

PopupWindow.showAtLocationを呼び出す場合、最初のパラメーターは、独自のビューではなく、その親ビューである必要があります。

代わりにこれを試してください:

// assuming R.id.linear is the 'android:id' of your main view
View mainLayout = container.findViewById(R.id.linear);
pw.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);

編集:将来の参考のために、次の2つのいずれかを実行してルートビューを取得することもできることに注意してください。

ステータスバーを含む

getWindow().getDecorView().findViewById(android.R.id.content)

ステータスバーを含まない

((ViewGroup)findViewById(android.R.id.content)).getChildAt(0)
于 2013-03-14T21:26:45.413 に答える