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:总述 最简单的视音频 ...
随机推荐
- c#:无法将类型为“System.DBNull”的对象强制转换为类型“System.String”
解决办法: 使用转换函数即可: Convert.ToString(要转换的值);
- 利用pandas进行数据分析之二:DataFrame与Series数据结构对比
Series与DataFrame对比学习 文章为本人原创,未经同意请勿转载,http://www.cnblogs.com/smallcrystal/ Series:构建的方法,一组数组(列表或元组), ...
- 计算机必知必会:进程process与线程thread 进程定义为一个正在运行的程序的实例
http://www.nowamagic.net/librarys/veda/detail/1741进程和线程这对概念的理解也是很难的,至今网络上可查的资料对其的理解出入都挺大,在不同的操作系统中,如 ...
- JS字符编码函数区别分析
http://www.jb51.net/article/14657.htm js对文字编码有3个函数: escape,encodeURI,encodeURIComponent, 对应的解码函数:une ...
- Spring boot配置fastjson
pom 文件引用 <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson< ...
- struts2 转发、重定向概述
转发等参数传递的注解方式: @Action(value = "operatorRoleAction", results = { @Result(name = "view& ...
- 转 springboot 教程
转 Spring Boot 揭秘与实战 系列 拓展阅读: https://zhuanlan.zhihu.com/dreawer?topic=Java 发表于 2016-12-21 | Spring框架 ...
- The value of 'filter_horizontal[0]' must be a many-to-many field. The value of 'raw_id_fields[0]' must be a foreign key or a many-to-many field.
在使用django admin的时候 我们遇到这个错误 <class 'Salesman.admin.UsrMngUserAdmin'>: (admin.E020) The value o ...
- Redis(七):Jedis简介和集群
Jedis简介 1.Jedis 是Redis 客户端工具jar2.使用非集群版示例代码 Jedis jedis = new Jedis("192.168.139.132", 637 ...
- Spring注解-@Configuration注解、@Bean注解以及配置自动扫描、bean作用域
1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.s ...