Android popupwindow 演示样例程序一
经过多番測试实践,实现了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 演示样例程序一的更多相关文章
- SNF快速开发平台MVC-各种级联绑定方式,演示样例程序(包含表单和表格控件)
做了这么多项目,经常会使用到级联.联动的情况. 如:省.市.县.区.一级分类.二级分类.三级分类.仓库.货位. 方式:有表单需要做级联的,还是表格行上需要做级联操作的. 实现:实现方法也有很多种方式. ...
- Tuxedo安装、配置、以及演示样例程序 (学习网址)
Tuxedo安装.配置.以及演示样例程序 (学习网址): 1.http://liu9403.iteye.com/blog/1415684 2.http://www.cnblogs.com/fnng/a ...
- Android模糊演示样例-RenderScript-附效果图与代码
本文链接 http://blog.csdn.net/xiaodongrush/article/details/31031411 參考链接 Android高级模糊技术 http://s ...
- Python Socket 编程——聊天室演示样例程序
上一篇 我们学习了简单的 Python TCP Socket 编程,通过分别写服务端和client的代码了解主要的 Python Socket 编程模型.本文再通过一个样例来加强一下对 Socket ...
- EXT-JS 6演示样例程序-Login演示样例程序
1. 用Sencha Cmd生成应用程序模版 sencha -sdk /path/to/ExtSDK generate app -classic TutorialApp./Tutoria ...
- Android之——流量管理程序演示样例
转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47680811 眼下.市面上有非常多管理手机流量的软件,能够让用户实时获取到自己手机 ...
- 构造Scala开发环境并创建ApiDemos演示样例项目
从2011年開始写Android ApiDemos 以来.Android的版本号也更新了非常多,眼下的版本号已经是4.04. ApiDemos中的样例也添加了不少,有必要更新Android ApiDe ...
- 函数指针使用演示样例(參考Linux-内核代码)
本文有xhz1234(徐洪志)编写,转载请注明出处. http://blog.csdn.net/xhz1234/article/details/36635083 作者:徐洪志 近期阅读Linux-内核 ...
- 最简单的视音频播放演示样例7:SDL2播放RGB/YUV
===================================================== 最简单的视音频播放演示样例系列文章列表: 最简单的视音频播放演示样例1:总述 最简单的视音频 ...
随机推荐
- mybatis WARN No appenders could be found for logger的解决方法
log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory).log4j:WARN ...
- python --curl重定向到文件范例
import sys import os import subprocess import time start = time.time() old=sys.stdout f=open('test ...
- Cocos2d-x3.0 载入Cocostudio的UI后,button无法点击的解决方法
原帖地址:http://blog.csdn.net/musicvs/article/details/28390617 近期发现不少朋友都遇到这个问题,用Cocostudio的UI编辑器创建好UI后,在 ...
- SQL server中的parsename详解
1.SQL server中如何拆分ip地址 比如有一个ip地址是 192.168.0.8 2.再或者,如何拆分一个包装比例,比如1:5:3 用parsename方法即可以实现: select pars ...
- Java Persistence with MyBatis 小结3
1 映射器配置文件和映射器接口 在 com.mybatis3.mappers 包中的 StudentMapper.xml 配置文件内,是如何配置 id 为”findStudentById”的 SQL ...
- map集合的循环示例
/** * 报警状态下拉列表信息 * @return * @throws Exception */ public String alarmStatusList() throws Exception { ...
- ajax提交数据处理总结
一:Ajax中Get请求与Post请求的区别 http://www.cnblogs.com/oneword/archive/2011/06/06/2073533.html 二:ajax分页: 360 ...
- @XStreamAlias使用
@XStreamAlias使用 一. 特点: 简化的API; 无映射文件; 高性能,低内存占用; 整洁的XML; 不需要修改对象;支持内部私有字段,不需要setter/getter方法 提供序列化接口 ...
- 每日英语:The Upside of Favoritism
As the head of the entertainment division of a major public-relations firm in New York, Janelle was ...
- 如何只利用NMAKE+CL+LINK写WIN32程序
关键是1.包含<Windows.h>及其他的相关头文件2.在LINK指令中最起码要加上KERNEL32.LIB USER32.LIB GDI32.LIB(不需要制定其路径,因为NMAKE, ...