经过多番測试实践,实现了popupwindow 弹出在指定控件的下方。代码上有凝视。有须要注意的地方。popupwindow 有自已的布局,里面控件的监听实现都有。接下来看代码实现。

项目资源下载:点击这里

TestPopwindow2.class

package com.example.popwindowdemo;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow; public class TestPopwindow2 extends PopupWindow {
// 根视图
private View mRootView;
// LayoutInflater
LayoutInflater mInflater; public TestPopwindow2(Activity context) {
super(context);
mInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mRootView = mInflater.inflate(R.layout.test_popwindow_2, null);
setContentView(mRootView); this.setWidth(LayoutParams.WRAP_CONTENT);
this.setHeight(LayoutParams.WRAP_CONTENT); // 设置PopUpWindow弹出的相关属性
setTouchable(true);
setOutsideTouchable(true);
setFocusable(true);
setBackgroundDrawable(new BitmapDrawable(context.getResources()));
update(); getContentView().setFocusableInTouchMode(true);
getContentView().setFocusable(true);
setAnimationStyle(R.style.AppBaseTheme);
}
}

MainActivity.class

package com.example.popwindowdemo;

import android.os.Bundle;
import android.app.Activity;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener,
OnDismissListener { private TestPopwindow2 mTestPopwindow2 = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); InitUI(); } private void InitUI() {
// 实例化TestPopwindow2
mTestPopwindow2 = new TestPopwindow2(this);
// 设置点击其它位置mTestPopwindow2消失
mTestPopwindow2.setOnDismissListener(this); Button buttonTest2 = (Button) findViewById(R.id.buttonTest2);
buttonTest2.setOnClickListener(this);
} private void OnPopwindowTest2() {
if (mTestPopwindow2 == null)
return; // location获得控件的位置
int[] location = new int[2];
View v = findViewById(R.id.buttonTest2);
if (v != null)
v.getLocationOnScreen(location); // 控件在屏幕的位置
mTestPopwindow2.setAnimationStyle(R.style.AppBaseTheme); // mTestPopwindow2弹出在某控件(button)的以下
mTestPopwindow2.showAtLocation(v, Gravity.TOP | Gravity.LEFT,
location[0] - v.getWidth(), location[1] + v.getHeight());
} // mTestPopwindow2布局控件的监听
public void OnclickTestListener(View view) {
switch (view.getId()) {
case R.id.layoutSeclect1:
Toast.makeText(this, "系统热门方案", Toast.LENGTH_SHORT).show();
break;
case R.id.layoutSeclect2:
Toast.makeText(this, "个人热门方案", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
if (mTestPopwindow2 != null)
mTestPopwindow2.dismiss();
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonTest2:
OnPopwindowTest2();
break;
default:
break;
}
} // 点击其它地方消失
@Override
public void onDismiss() {
}
}

test_popwindow_2.xml

<?

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:background="#0000" > <!--
popupwimdow的布局注意不能使用相似
android:layout_marginTop=""这样属性,特别是相对布局时候.
布局或者控件总体靠左等都会影响popupwimdow定位使用
--> <LinearLayout
android:id="@+id/hot_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/title_function_bg"
android:orientation="vertical" > <LinearLayout
android:id="@+id/layoutSeclect1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="OnclickTestListener" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="系统热门方案"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout> <LinearLayout
android:id="@+id/layoutSeclect2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="OnclickTestListener" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="8dp"
android:text="个人热门方案"
android:textColor="#fff"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout> </LinearLayout>

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayoutMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"> <Button
android:id="@+id/buttonTest2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="測试2" />
</LinearLayout> </LinearLayout>

效果图例如以下

项目资源下载:点击这里

转载请注明出处的博客网址: http://blog.csdn.net/qq_16064871

如有转载未注明出处博客网址,或者没有得到作者的允许。

作者持有版权全部权。

Android popupwindow 演示样例程序一的更多相关文章

  1. SNF快速开发平台MVC-各种级联绑定方式,演示样例程序(包含表单和表格控件)

    做了这么多项目,经常会使用到级联.联动的情况. 如:省.市.县.区.一级分类.二级分类.三级分类.仓库.货位. 方式:有表单需要做级联的,还是表格行上需要做级联操作的. 实现:实现方法也有很多种方式. ...

  2. Tuxedo安装、配置、以及演示样例程序 (学习网址)

    Tuxedo安装.配置.以及演示样例程序 (学习网址): 1.http://liu9403.iteye.com/blog/1415684 2.http://www.cnblogs.com/fnng/a ...

  3. Android模糊演示样例-RenderScript-附效果图与代码

    本文链接    http://blog.csdn.net/xiaodongrush/article/details/31031411 參考链接    Android高级模糊技术    http://s ...

  4. Python Socket 编程——聊天室演示样例程序

    上一篇 我们学习了简单的 Python TCP Socket 编程,通过分别写服务端和client的代码了解主要的 Python Socket 编程模型.本文再通过一个样例来加强一下对 Socket ...

  5. EXT-JS 6演示样例程序-Login演示样例程序

    1.        用Sencha Cmd生成应用程序模版 sencha -sdk /path/to/ExtSDK generate app -classic TutorialApp./Tutoria ...

  6. Android之——流量管理程序演示样例

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47680811 眼下.市面上有非常多管理手机流量的软件,能够让用户实时获取到自己手机 ...

  7. 构造Scala开发环境并创建ApiDemos演示样例项目

    从2011年開始写Android ApiDemos 以来.Android的版本号也更新了非常多,眼下的版本号已经是4.04. ApiDemos中的样例也添加了不少,有必要更新Android ApiDe ...

  8. 函数指针使用演示样例(參考Linux-内核代码)

    本文有xhz1234(徐洪志)编写,转载请注明出处. http://blog.csdn.net/xhz1234/article/details/36635083 作者:徐洪志 近期阅读Linux-内核 ...

  9. 最简单的视音频播放演示样例7:SDL2播放RGB/YUV

    ===================================================== 最简单的视音频播放演示样例系列文章列表: 最简单的视音频播放演示样例1:总述 最简单的视音频 ...

随机推荐

  1. Linux中内存挂载到目录下

    [日期:2012-11-14]   /dev/shm是linux下的一块共享内存结构.默认大小是真实内存的一半.它用来存储进程间通讯时的一些共享数据结构.在物理内存足够时,会在内存中进行数据交换,如果 ...

  2. 最大似然估计(MLE)与最大后验概率(MAP)

    何为:最大似然估计(MLE): 最大似然估计提供了一种给定观察数据来评估模型参数的方法,即:“模型已定,参数未知”.可以通过采样,获取部分数据,然后通过最大似然估计来获取已知模型的参数. 最大似然估计 ...

  3. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  4. 新浪微博XSS攻击事件

    http://blog.csdn.net/terryzero/article/details/6575078 6月28日20时14分左右开始,新浪微博出现了一次比较大的XSS攻击事件.大量用户自动发送 ...

  5. 把一个一中的字段更新另一个表中的t-sql

    UPDATE dbo.CommDescr SET Descr=(SELECT ba.content FROM dbo.blog_article ba WHERE ba.id=3) WHERE Comm ...

  6. EL表达式中fn函数

    JSTL 使用表达式来简化页面的代码,这对一些标准的方法,例如bean的getter/setter方法,请求参数或者context以及 session中的数据的访问非常方便,但是我们在实际应用中经常需 ...

  7. C++知识点 笔试常见

    C++知识点   一.#include “filename.h”和#include <filename.h>的区别 #include “filename.h”是指编译器将从当前工作目录上开 ...

  8. CControlLayer

    #ifndef __CONTROLLAYER_H__ #define __CONTROLLAYER_H__ #include "XWidget.h" class CMapDialo ...

  9. 验分享:CSS浮动(float,clear)通俗讲解

    经验分享:CSS浮动(float,clear)通俗讲解 很早以前就接触过CSS,但对于浮动始终非常迷惑,可能是自身理解能力差,也可能是没能遇到一篇通俗的教程. 前些天小菜终于搞懂了浮动的基本原理,迫不 ...

  10. 访问子节点childNodes

    访问子节点childNodes 访问选定元素节点下的所有子节点的列表,返回的值可以看作是一个数组,他具有length属性. 语法: elementNode.childNodes 注意: 如果选定的节点 ...