Android PopupWindow使用实例

所属分类: 软件编程 / Android 阅读数: 466
收藏 0 赞 0 分享

示例效果如下:

 

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实现信号强度监听的方法

这篇文章主要介绍了Android实现信号强度监听的方法,是Android手机中很常见的一个实用功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Android实现Activity界面切换添加动画特效的方法

这篇文章主要介绍了Android实现Activity界面切换添加动画特效的方法,非常实用的技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中Dialog去黑边的方法

这篇文章主要介绍了Android中Dialog去黑边的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Qt for Android开发实例教程

这篇文章主要介绍了Qt for Android开发的方法,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之时间日期操作实例

这篇文章主要介绍了Android开发之时间日期操作,是Android程序开发中常见的一个功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之时间日期组件用法实例

这篇文章主要介绍了Android开发之时间日期组件用法,主要介绍了TimePicker和DatePicker组件,对于Android程序开发有不错的借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之获取网络链接状态

这篇文章主要介绍了Android获取网络链接状态的方法,主要是通过ConnectivityManager类来完成的,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之广播机制浅析

这篇文章主要介绍了Android开发之广播机制浅析,主要包括了发布、接收及配置广播的实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之登录验证实例教程

这篇文章主要介绍了Android开发之登录验证实现方法,包括发送数据、服务器端验证、配置文件等,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发之注册登录方法示例

这篇文章主要介绍了Android开发的注册登录方法,是针对Android程序设计中版本兼容性的进一步完善,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多