zoukankan      html  css  js  c++  java
  • 使用PopupWindow

        PopupWindow可以创建类似对话框风格的窗口,使用PopupWindow创建对话框风格的窗口只要如下两步即可:

    1. 调用PopupWindow的构造器创建PopupWindow对象。
    2. 调用PopupWindow的showAsDropDown(View v)将PopupWindow作为v组件的下拉组件显示出来;或调用PopupWindow的showAtLocation()方法将PopupWindow在指定位置显示出来。

         下面的程序示范了如何使用PopupWindow创建对话框风格的窗口。该程序的主程序中只有一个简单的按钮,用户单击该按钮时将会显示PopupWindow;其中PopupWindow负责加载并显示前一个示例的窗口界面。

          布局文件如下:

       

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_horizontal"
       >
    <Button android:id="@+id/bn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="弹出Popup窗口"/>
    </LinearLayout>

    后台Activity代码文件如下:

    package org.crazyit.helloworld;
    
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.PopupWindow;
    
    public class PopupWindowTest extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.popup_window_test);
            //装载R.layout.popup对应的界面布局
            View root=this.getLayoutInflater().inflate(R.layout.popup, null);
            //创建PopupWindow对象
            final PopupWindow popup=new PopupWindow(root,380,660);
            Button button=(Button)findViewById(R.id.bn);
            button.setOnClickListener(new OnClickListener(){
    
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //以下拉方式显示
                    popup.showAsDropDown(v);
                }
                
            });
            //获取PopupWindow的“关闭”按钮
            root.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    //关闭PopupWindow
                    popup.dismiss();
                }
            });
        }
    
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.popup_window_test, menu);
            return true;
        }
    
    }

    上面代码中提到的R.layout.popup界面布局文件如下:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
         > <Button android:id="@+id/close"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="关闭"/>
        <ImageView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/shuangzi"/>
    </LinearLayout>

    运行上面的程序将看到如下界面:

      

  • 相关阅读:
    HDU 3879 Base Station 最大权闭合图
    自己定义头像处理,轻巧有用,非常多强大的小技巧在里面哦,快来赞美我一下吧^_^
    【Spring实战】—— 8 自动装配
    【Spring实战】—— 7 复杂集合类型的注入
    【Spring实战】—— 6 内部Bean
    【Spring实战】—— 5 设值注入
    【Spring实战】—— 4 Spring中bean的init和destroy方法讲解
    【Spring实战】—— 3 使用facotry-method创建单例Bean总结
    【Spring实战】—— 2 构造注入
    【Spring实战】—— 1 入门讲解
  • 原文地址:https://www.cnblogs.com/wolipengbo/p/3394515.html
Copyright © 2011-2022 走看看