zoukankan      html  css  js  c++  java
  • Android UI开发第三篇——popupwindow

    PopupWindow在android.widget包下,弹出窗口的形式展示。官方文档对该控件的描述是:“一个弹出窗口控件,可以用来显示任意视图(View),而且会浮动在当前 活动(activity)的顶部”。PopupWindow可以让我们实现多种自定义控件,例如:menu、alertdialog等弹窗似的View。

          popupwindow需要在onCreate时初始化、通过触发事件展示出来。

    public class ShowPopupWindow extends Activity implements View.OnClickListener{

    View view;
    PopupWindow pop;
    Button btnShowAsDrawDown;
    Button btnShowAsDrawDown1;
    Button btnShowAtLocation;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.popup_activity);
    btnShowAsDrawDown = (Button)findViewById(R.id.btnShowAsDrawDown);
    btnShowAsDrawDown.setOnClickListener(this);
    btnShowAsDrawDown1 = (Button)findViewById(R.id.btnShowAsDrawDown1);
    btnShowAsDrawDown1.setOnClickListener(this);
    btnShowAtLocation = (Button)findViewById(R.id.btnShowAt);
    btnShowAtLocation.setOnClickListener(this);
    initPopupWindow();
    }

    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId())
    {
    case R.id.btnShowAsDrawDown:
    if(pop.isShowing())
    {
    pop.dismiss();
    }
    else
    {
    pop.showAsDropDown(v);
    }
    break;
    case R.id.btnShowAsDrawDown1:
    if(pop.isShowing())
    {
    pop.dismiss();
    }
    else
    {
    pop.showAsDropDown(v, 0, -160);
    }
    break;
    default:
    if(pop.isShowing())
    {
    pop.dismiss();
    }
    else
    {
    pop.showAtLocation(findViewById(R.id.main), Gravity.CENTER_HORIZONTAL, 0, 0);  
    }
    break;
    }


    }
    private void initPopupWindow()
    {
    view = this.getLayoutInflater().inflate(R.layout.popup_window, null);
    pop = new PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    pop.setOutsideTouchable(true);
    view.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    pop.dismiss();
    }
    });
    }

    }

    popup_activity.xml

    <?xml version="1.0" encoding="utf-8"?>  
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
        android:orientation="vertical" 
        android:id="@+id/main" 
        android:layout_width="fill_parent" 
        android:layout_height="fill_parent">  
    <TextView    
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:text="pop demo!" 
        />  
    <Button    
        android:id="@+id/btnShowAsDrawDown" 
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:text="Show as drawndown(one parameter)"
        />  
        <Button    
        android:id="@+id/btnShowAsDrawDown1" 
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:text="Show as drawndown(three parameters)"
        /> 
        <Button    
        android:id="@+id/btnShowAt" 
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:text="Show At Location"
        />
    </LinearLayout> 

    popup_window.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"
      android:gravity="center_horizontal"
      android:background="#d3d3d3">
        <Button
    android:id="@+id/btn_pop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
                            android:text="Close"
    android:layout_margin="10dip" />
    </LinearLayout>
     
     
    /**
    * @author 张兴业
    * 邮箱:xy-zhang@163.com
    * qq:363302850
    *
    */
  • 相关阅读:
    【codecombat】 试玩全攻略 第九关 循环又循环
    【codecombat】 试玩全攻略 第十三关 已知敌人
    【codecombat】 试玩全攻略 第十一关 再次迷宫经历
    【codecombat】 试玩全攻略 第六关 cell commentary
    【codecombat】 试玩全攻略 第八关 火舞
    【codecombat】 试玩全攻略 第十二关 恐惧之门
    【codecombat】 试玩全攻略 第十四关 已知敌人
    苹果apns推送总结
    Xcode 提升速度小技巧
    UITextField 限制输入字数
  • 原文地址:https://www.cnblogs.com/xyzlmn/p/3168177.html
Copyright © 2011-2022 走看看