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. Mysql 的一些基本用法

    一.增加字段 COMMENT '是否导入基础信息平台 1 是导入'; 二.删除字段 alter table `provincestudentinfo` drop column NativePlace; ...

  2. 虚拟rethat联网问题

    近日在vmware虚拟了一台rethat的linux,但是使用桥连也上不了网! 宿主机是win7,由于在公司,是采用固定ip上网的,通过上网查资料,终于可以连接网络了: 步骤如下: 第一:修改vi / ...

  3. js中的总结汇总(以后的都收集到这篇)

    点1:js中的比较字符串是否相等,js中是用"=="这个来判断是否相等,这点跟java中不一样,java中是.equals()这种方法. 在之前写的ajax的demo中,因为用了. ...

  4. Hbase 配置问题(ERROR: org.apache.hadoop.hbase.PleaseHoldException: org.apache.hadoop.hbase.PleaseHoldEx)

    ERROR: org.apache.hadoop.hbase.PleaseHoldException: org.apache.hadoop.hbase.PleaseHoldException: Mas ...

  5. media_root以及static_root配置

    # At the top of settings/base.pyfrom os.path import join, abspath, dirnamehere = lambda *x: join(abs ...

  6. 使用adb签名并安装Android程序

    首先需要准备Android SDK包,我是在windows上操作的,在PATH中配置  YOUT_SDK_PATH\android-sdk-windows\platform-tools 和  YOUT ...

  7. hpu第五届acm比赛

    vijos P1211生日日数   描述 CCC老师的生日是YY年MM月DD日,他想知道自己出生后第一万天纪念日的日期(出生日算第0天). 格式 输入格式 从文件的第一行分别读入YY,MM,DD其中1 ...

  8. 自己设计的SSO登录流程图

    这个图上不考虑安全加密.由于本身SSO流程图已经比較复杂了.可能还有问题,欢迎大家拍砖. 1.登录流程图: 2.退出流程图: 3.改进方面: 每一个应用登录后.直接将ticket写入session中, ...

  9. Ubuntu 12.04 安装JDK 8和Eclipse

    Ubuntu 12.04 下安装 JDK8 方法一:(缺点是安装时附加openjdk等大量程序并无法去除,长处是安装简单) $ sudo apt-get install eclipse 方法二:(长处 ...

  10. LeetCodeOJ. String to Integer (atoi)

    试题请參见: https://oj.leetcode.com/problems/string-to-integer-atoi/ 题目概述 Implement atoi to convert a str ...