2013-03-14 180 views
0

我想从我的一个滑动视图(扩展片段的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(); 
     } 
    }; 

} 

回答

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); 

编辑:为了将来参考,请记住,你也可以做两两件事之一获取根查看:

包括状态栏

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

不包括状态栏

((ViewGroup)findViewById(android.R.id.content)).getChildAt(0) 
+0

谢谢,我只是不完全明白你的意思。你的意思是例如线性布局的ID从哪里被调用? – 2013-03-14 21:57:59

+0

是的,LinearLayout是PopupWindow的父视图。 – crazylpfan 2013-03-14 22:02:48

+0

该死的,我把它改为View layout = inflater.inflate(R.layout.popup_layout, \t(ViewGroup)container.findViewById(R.id.linear)); \t \t pw = new PopupWindow(layout,300,470,true); \t \t pw.showAtLocation(layout,Gravity.CENTER,0,0); ...但它仍然不显示。 id线性现在是来自layout_two.xml(我基于Munish Kapoor的示例:http://manishkpr.webheavens.com/android-viewpager-example/)的主要LinearLayout的id,如果我用R替换布局。 id.linear我得到一个错误,这不是一个视图.. – 2013-03-14 22:10:46