Android PopupWindow使用实例

 更新时间:2015年06月09日 10:59:45   投稿:junjie  
这篇文章主要介绍了Android PopupWindow使用实例,本文直接给出代码实例,需要的朋友可以参考下

示例效果如下:

 

MainActivity.xml

package sn.qdj.popupwindowdemo;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow;
/**
 * PopupWindow使用
 * @author qingdujun
 *
 */
public class MainActivity extends ActionBarActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    /**
     * popup.xml 为弹出界面布局
     */
    View root = getLayoutInflater().inflate(R.layout.popup, null);
    /**
     * 弹出界面
     * 宽度:400
     * 高度:200
     */
    final PopupWindow popup = new PopupWindow(root, 400,200);
    Button btn = (Button)findViewById(R.id.btn);
    Button close = (Button)findViewById(R.id.close);
    btn.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
  // TODO Auto-generated method stub
  /**
   * 在指定位置弹出
   * 
   * 第一个参数指定PopupWindow的锚点view,即依附在哪个view上。
   * 第二个参数指定起始点为parent的右下角
   * 第三个参数设置以btn的下方为原点,向左、上各偏移0像素。
   */
  popup.showAtLocation(findViewById(R.id.btn), Gravity.BOTTOM, 0, 0);
  }
 });
    close.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
  // TODO Auto-generated method stub
  /**
   * 关闭PopupWindow
   */
  popup.dismiss();
  }
 });
  }
}


activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent" >
  <Button
    android:id="@+id/btn"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:text="弹出" />
  <Button
    android:id="@+id/close"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
 android:layout_toRightOf="@id/btn"
    android:text="关闭" />
</RelativeLayout>

popup.xml

<?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/btn1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="相册" />
  <Button
    android:id="@+id/btn2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="拍照" />
</LinearLayout>

相关文章

  • Android中layout属性大全

    Android中layout属性大全

    这篇文章主要介绍了Android中layout属性含义及用法,较为详细的总结分析了layout属性相关用法,需要的朋友可以参考下
    2015-05-05
  • android studio集成unity导出工程的实现

    android studio集成unity导出工程的实现

    本文主要介绍了android studio集成unity导出工程的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • 详解Android aidl的使用方法

    详解Android aidl的使用方法

    AIDL是Android中IPC(Inter-Process Communication)方式中的一种,AIDL是Android Interface definition language的缩写。这篇文章主要介绍了Android aidl的使用方法,感兴趣的朋友跟随小编一起看看吧
    2020-07-07
  • 详解Android MVP开发模式

    详解Android MVP开发模式

    这篇文章主要为大家介绍了Android MVP开发模式的相关资料,需要的朋友可以参考下
    2016-01-01
  • Android编程实现获得手机屏幕真实宽高的方法

    Android编程实现获得手机屏幕真实宽高的方法

    这篇文章主要介绍了Android编程实现获得手机屏幕真实宽高的方法,以两个实例形式分析了获取手机屏幕信息的相关技巧,需要的朋友可以参考下
    2015-10-10
  • Android UI设计系列之自定义EditText实现带清除功能的输入框(3)

    Android UI设计系列之自定义EditText实现带清除功能的输入框(3)

    这篇文章主要介绍了Android UI设计系列之自定义EditText实现带清除功能的输入框,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2016-06-06
  • Android中dip、dp、sp、pt和px的区别详解

    Android中dip、dp、sp、pt和px的区别详解

    本篇文章是对Android中dip、dp、sp、pt和px的区别进行了详细的分析介绍,需要的朋友参考下
    2013-06-06
  • flutter 常见圆角处理示例详解

    flutter 常见圆角处理示例详解

    这篇文章主要介绍了flutter 常见圆角处理示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-03-03
  • Android开发Compose框架使用开篇

    Android开发Compose框架使用开篇

    这篇文章主要为大家介绍了Android开发Compose框架使用开篇,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • Android 自定义TextView去除paddingTop和paddingBottom

    Android 自定义TextView去除paddingTop和paddingBottom

    这篇文章主要介绍了Android 自定义TextView去除paddingTop和paddingBottom的相关资料,这里提供实例来帮助大家实现这样的功能,需要的朋友可以参考下
    2017-09-09

最新评论