MainActivity.java

<span style="font-size:14px;">package com.main;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow; public class MainActivity extends Activity { private static final String TAG = "MainActivity";
private Button button; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// 显示 popupWindow
PopupWindow popupWindow = makePopupWindow(MainActivity.this);
int[] xy = new int[2];
button.getLocationOnScreen(xy);
popupWindow.showAtLocation(button, Gravity.RIGHT | Gravity.TOP,
-xy[0] / 2, xy[1] + button.getWidth());
// popupWindow.showAsDropDown(button,0, 0);
}
});
} // 创建一个包括自己定义view的PopupWindow
private PopupWindow makePopupWindow(Context cx) {
PopupWindow window;
window = new PopupWindow(cx); // View contentView =
// LayoutInflater.from(this).inflate(R.layout.popwindow, null);
// window.setContentView(contentView);
Button b1 = new Button(this);
b1.setText("first");
b1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT)); Button b2 = new Button(this);
b2.setText("Second");
b2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT)); LinearLayout linearLayout = new LinearLayout(this);
linearLayout.addView(b1);
linearLayout.addView(b2);
linearLayout.setOrientation(LinearLayout.VERTICAL); window.setContentView(linearLayout);
window.setBackgroundDrawable(getResources().getDrawable(
R.drawable.ic_launcher));
// window.setWidth(DisplayManager.dipToPixel(150));
// window.setHeight(DisplayManager.dipToPixel(150));
window.setWidth(150);
window.setHeight(150); // 设置PopupWindow外部区域是否可触摸
window.setFocusable(true); // 设置PopupWindow可获得焦点
window.setTouchable(true); // 设置PopupWindow可触摸
window.setOutsideTouchable(true); // 设置非PopupWindow区域可触摸
return window;
}
}</span>

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

<span style="font-size:14px;"><?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" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:orientation="horizontal" > <TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Title" /> <Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click" />
</LinearLayout> </LinearLayout></span>

王立平--PopupWindow的更多相关文章

  1. 王立平--TableLayout

    效果: <?xml version="1.0" encoding="utf-8"?>  <TableLayout xmlns:android= ...

  2. 王立平--EditPlus激活码

    注冊名:Free User 注冊码:6AC8D-784D8-DDZ95-B8W3A-45TFA

  3. 王立平-Android中对图像进行Base64编码

    // ------------------base64-------------------// public String bitmaptoString(Bitmap bitmap) { // 将B ...

  4. 王立平--android事件监听的3种方式

    第一种通常在activity组件的oncreate事件中直接定义,直接动作. 这样的方式每一个控件都定义一次.通常不方便. Button btn = (Button) findViewById(R.i ...

  5. 王立平-- ContentValues , HashTable , HashMap差别

    ContentValues  :是一种存储机制,key-value 特点:key仅仅能是string类型.value:仅仅能是基本类型,不能是对象. 应用:经常使用语往数据库中插入数据 Content ...

  6. 王立平--eclipse向svnserver上传项目

    1.team-->share project watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxMzQyNTUyNw==/font/5a6L5L2 ...

  7. 王立平--android中的anim(动画)

    简单有用步骤: 1.新建anim目录. 2.在anim下新建xml文件, 3.在xml下编写自己须要动画. 简单样例: 给Imageview加入动画 public class MainActivity ...

  8. 王立平--Unity中间GUI Skin

    C#文字: public class NewBehaviourScript2 : MonoBehaviour { public Texture t; public GUISkin skin; // U ...

  9. 王立平--Failed to push selection: Read-only file system

    往android模拟器导入资源,失败. 提示:仅仅读文件. mnt是仅仅读文件.应点击sdcard.,在导入

随机推荐

  1. 利用CentOS系统IPtables防火墙添加网站IP白名单

    参考博文: 利用CentOS系统IPtables防火墙添加360网站卫士节点IP白名单 centos6.5添加白名单如下: 在防火墙 配置文件中加入白名单  ip -A INPUT -s 183.13 ...

  2. Java-Math

    java.lang.Math类提供的方法都是static的,“静态引入 ”使得不必每次在调用类方法时都在方法前写上类名:             import static java.lang.Mat ...

  3. 部署ASP.NET MVC项目

    目标:了解部署过程,掌握部署中出现问题该如何处理. 部署网站往往是一件麻烦事,因为在安装部署的过程中,经常有许多步骤要运行,对于许多不太熟悉IIS/SQL的新手来说,部署网站编程一件非常困难且危险的事 ...

  4. 使用线程新建WPF窗体(公用进度条窗体)

    使用线程新建窗体 项目中需要一个公用的进度条窗体.大家知道在wpf中,有两个线程,一个是UI线程,另一个是监听线程(一直监听用户的输入).如果我们后台有阻塞UI线程的计算存在,那么界面上的比如进度条什 ...

  5. hdu 1251 统计难题 初识map

    Problem Description Ignatius近期遇到一个难题,老师交给他非常多单词(仅仅有小写字母组成,不会有反复的单词出现),如今老师要他统计出以某个字符串为前缀的单词数量(单词本身也是 ...

  6. 达内TTS6.0课件basic_day05

  7. Linux看门狗脚本 1.4

    近期项目的看门狗经历了三个版本号. 第一个版本号: 用ps -ef,假设程序挂了就启动 第二个版本号: 程序因为执行时会出现不再监听7901port,所以不能简单推断机器是不是挂了,而是推断此port ...

  8. java--多线程之Thread继承

    多线程,是java的特殊机制.所谓线程就是程序执行的流程.“多线程”就是可以在同一时刻能够执行多个程序块(注意,是程序块,而不是程序),这样一来就可以使得程序的执行速度大大增加. package Te ...

  9. android-studio 安装gradle

    http://services.gradle.org/distributions 下载需要的gradle 放到C:\Users\Administrator\.gradle\wrapper\dists\ ...

  10. Android常用秘籍总结

    一.无法向模拟器push文件,显示read-only file system $adb shell mount -o remount rw/ 确保模拟器有sd卡 二.Android模拟按键 #adb ...