2011-05-10 212 views
29

要创建一个简单的工作PopupWindow,我们需要做到以下几点:PopupWindow android系统

popup_example.xml:

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="vertical" 
     android:padding="10dip" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content"> 

     <TextView   
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="10dip" 
      android:text="Test Pop-Up" /> 

    </LinearLayout> 

Java代码

LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.popup_example, null, false),100,100, true); 

pw.showAtLocation(this.findViewById(R.id.main), Gravity.CENTER, 0, 0); 

我的要求是我需要一个

<TEXTVIEW android:layout_height="wrap_content" android:layout_width="fill_parent" /> 

<BUTTON android:id="@+id/end_data_send_button" android:text="Cancel"/> 
popup_example.xml

。我如何在Java代码中处理这两个组件?

screenshot

+6

根据您展示什么,这大概应该是对话,而不是一个PopupWindow。 – 2012-02-22 12:17:30

+3

...现在比以往任何时候都更多的是DialogFragments可用http://developer.android.com/guide/topics/ui/dialogs.html – samosaris 2012-12-03 14:00:16

+0

嘿,我可以使用弹出窗口来显示放大的图像(上点击图片),这是动态显示在列表视图中?谢谢。 – 2015-04-22 17:14:22

回答

51

在这里,我给你一个演示的例子。看到这个并根据你的需要进行定制。

public class ShowPopUp extends Activity { 

    PopupWindow popUp; 
    LinearLayout layout; 
    TextView tv; 
    LayoutParams params; 
    LinearLayout mainLayout; 
    Button but; 
    boolean click = true; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     popUp = new PopupWindow(this); 
     layout = new LinearLayout(this); 
     mainLayout = new LinearLayout(this); 
     tv = new TextView(this); 
     but = new Button(this); 
     but.setText("Click Me"); 
     but.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
     if (click) { 
     popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10); 
     popUp.update(50, 50, 300, 80); 
     click = false; 
     } else { 
     popUp.dismiss(); 
     click = true; 
     } 
     } 

     }); 
     params = new LayoutParams(LayoutParams.WRAP_CONTENT, 
     LayoutParams.WRAP_CONTENT); 
     layout.setOrientation(LinearLayout.VERTICAL); 
     tv.setText("Hi this is a sample text for popup window"); 
     layout.addView(tv, params); 
     popUp.setContentView(layout); 
     // popUp.showAtLocation(layout, Gravity.BOTTOM, 10, 10); 
     mainLayout.addView(but, params); 
     setContentView(mainLayout); 
    } 
    } 

希望这能解决您的问题。

2
Button endDataSendButton = (Button)findViewById(R.id.end_data_send_button); 

同样可以通过添加一个ID得到它的文本视图。

+0

@ user746108 pw.showAtLocation(this.findViewById(R.id.text_view),Gravity.CENTER,0,0); TextView只显示在弹出窗口中..我如何修复按钮位置 – jennifer 2011-05-10 04:04:36

4

你完成了布局充气?也许你可以试试这个!

View myPoppyView = pw.getContentView(); 
Button myBelovedButton = (Button)myPoppyView.findViewById(R.id.my_beloved_button); 
//do something with my beloved button? :p 
4

我构建了自己的类,然后从我的活动中调用它,覆盖诸如showAtLocation之类的小方法。当我在我的活动中有4到5个弹出窗口时,我发现它更容易。

public class ToggleValues implements OnClickListener{ 

    private View pView; 
    private LayoutInflater inflater; 
    private PopupWindow pop; 
    private Button one, two, three, four, five, six, seven, eight, nine, blank; 
    private ImageButton eraser; 
    private int selected = 1; 
    private Animation appear; 

    public ToggleValues(int id, Context c, int screenHeight){ 
     inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     pop = new PopupWindow(inflater.inflate(id, null, false), 265, (int)(screenHeight * 0.45), true); 
     pop.setBackgroundDrawable(c.getResources().getDrawable(R.drawable.alpha_0)); 
     pView = pop.getContentView(); 

     appear = AnimationUtils.loadAnimation(c, R.anim.appear); 

     one = (Button) pView.findViewById(R.id.one); 
     one.setOnClickListener(this); 
     two = (Button) pView.findViewById(R.id.two); 
     two.setOnClickListener(this); 
     three = (Button) pView.findViewById(R.id.three); 
     three.setOnClickListener(this); 
     four = (Button) pView.findViewById(R.id.four); 
     four.setOnClickListener(this); 
     five = (Button) pView.findViewById(R.id.five); 
     five.setOnClickListener(this); 
     six = (Button) pView.findViewById(R.id.six); 
     six.setOnClickListener(this); 
     seven = (Button) pView.findViewById(R.id.seven); 
     seven.setOnClickListener(this); 
     eight = (Button) pView.findViewById(R.id.eight); 
     eight.setOnClickListener(this); 
     nine = (Button) pView.findViewById(R.id.nine); 
     nine.setOnClickListener(this); 
     blank = (Button) pView.findViewById(R.id.blank_Selection); 
     blank.setOnClickListener(this); 
     eraser = (ImageButton) pView.findViewById(R.id.eraser); 
     eraser.setOnClickListener(this); 
    } 

    public void showAtLocation(View v) { 
     pop.showAtLocation(v, Gravity.BOTTOM | Gravity.LEFT, 40, 40); 
     pView.startAnimation(appear); 
    } 

    public void dismiss(){ 
     pop.dismiss(); 
    } 

    public boolean isShowing() { 
     if(pop.isShowing()){ 
      return true; 
     }else{ 
      return false; 
     } 
    } 

    public int getSelected(){ 
     return selected; 
    } 

    public void onClick(View arg0) { 
     if(arg0 == one){ 
      Sudo.setToggleNum(1); 
     }else if(arg0 == two){ 
      Sudo.setToggleNum(2); 
     }else if(arg0 == three){ 
      Sudo.setToggleNum(3); 
     }else if(arg0 == four){ 
      Sudo.setToggleNum(4); 
     }else if(arg0 == five){ 
      Sudo.setToggleNum(5); 
     }else if(arg0 == six){ 
      Sudo.setToggleNum(6); 
     }else if(arg0 == seven){ 
      Sudo.setToggleNum(7); 
     }else if(arg0 == eight){ 
      Sudo.setToggleNum(8); 
     }else if(arg0 == nine){ 
      Sudo.setToggleNum(9); 
     }else if(arg0 == blank){ 
      Sudo.setToggleNum(0); 
     }else if(arg0 == eraser){ 
      Sudo.setToggleNum(-1); 
     } 
     this.dismiss(); 
    } 

} 
+0

如何从按钮点击操作中调用此ToggleValues类 – 2012-12-24 12:47:39

3
LayoutInflater inflater = (LayoutInflater) SettingActivity.this.getSystemService(SettingActivity.LAYOUT_INFLATER_SERVICE); 
PopupWindow pw = new PopupWindow(inflater.inflate(R.layout.gd_quick_action_slide_fontsize, null),LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT, true); 
pw.showAtLocation(SettingActivity.this.findViewById(R.id.setting_fontsize), Gravity.CENTER, 0, 0); 
View v= pw.getContentView(); 
TextView tv=v.findViewById(R.id.....);